0

I got a class that looks like this:

public class MyClass
{
    int myNum;

    private MyClass() {}

    public static MyClass CreateInstance()
    {
        MyClass a = new MyClass();
        a.myNum=5;
        return a;
    }

    public bool IsBigger(MyClass b)
    {
        return this.myNum > b.myNum;
    }

}

then, i want to make a shim of it and want to use the IsBigger method, but by default it returns false. How do i call the base method in this instance case ?

The test goes like this:

[TestMethod]
Public void test()
{
 ShimMyClass firstShim = new ShimMyClass();
 firstShim.myNumGet = () => { return 6; }

 ShimMyClass secondShim = new ShimMyClass();
 secondShim.myNumGet = () => { return 7; }

 Assert.IsTrue(secondShim.Instance.IsBiggerThan(firstShim.Instance);
}
Lironess
  • 823
  • 1
  • 11
  • 19
  • 1
    Show how you call the IsBigger-method. And what do you mean with base-method? There is no base-method in my opinion... – Abbas Oct 09 '12 at 12:36
  • I meN than with unit test i create a shim instance and set the myNum to other numbers. Than i call firstShimMyClass.Instance.IsBiggerThan(otherShimClass), but it returns falls even if i didnt implemented it – Lironess Oct 09 '12 at 12:39

1 Answers1

0

Make MyNum a Property:

public class MyClass
{
    public int MyNum { get; private set; }

    private MyClass() {}

    public static MyClass CreateInstance()
    {
        MyClass a = new MyClass();
        a.MyNum=5;
        return a;
    }

    public bool IsBigger(MyClass b)
    {
        return this.MyNum > b.MyNum;
    }

}

Create Shim Context before using the ShimMyClass:

using (ShimsContext.Create())
{ 
    ShimMyClass firstShim = new ShimMyClass();
     firstShim.MyNumGet = () => { return 6; }

     ShimMyClass secondShim = new ShimMyClass();
     secondShim.MyNumGet = () => { return 7; }

     Assert.IsTrue(secondShim.IsBiggerThan(firstShim);
}

Microsoft Fakes on MSDN

oberfreak
  • 1,799
  • 13
  • 20