Howdy, I am wondering if there is any shorthand for mocking an object with Members.MustSpecifyReturnValues, but still have a property with getter/setter act as an autoproperty and not call the underlying implementation.
For instance:
public class TestMe
{
public MockMe Mock;
public TestMe(MockMe mock) { Mock = mock; }
public void PrintMockA()
{
Console.WriteLine(Mock.A);
}
}
public class MockMe
{
public int A { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
public void PrintA()
{
Console.WriteLine(A);
}
}
[Test]
public void Works()
{
var mockMe = Isolate.Fake.Instance<MockMe>(Members.MustSpecifyReturnValues);
// I want to achieve this without writing such verbose code
int mockMe_a = 0;
Isolate.WhenCalled(() => mockMe.A).DoInstead(context => mockMe_a);
Isolate.WhenCalled(() => mockMe.A = 0).DoInstead(context => mockMe_a = (int) context.Parameters[0]);
mockMe.A = 42;
Console.WriteLine(mockMe.A);
mockMe.PrintA();
var testMe = new TestMe(mockMe);
testMe.PrintMockA();
}
[Test]
public void DontWork()
{
var mockMe = Isolate.Fake.Instance<MockMe>(Members.MustSpecifyReturnValues);
mockMe.A = 42;
Console.WriteLine(mockMe.A);
mockMe.PrintA();
var testMe = new TestMe(mockMe);
// throws NullReferenceException
testMe.PrintMockA();
}
I basically want to achieve the functionality of Works() without having to write such verbose code (I'm lazy, and also there are a lot of these properties).
Strangely, in DontWork(), mockMe.A does act as an auto-property... except where I most want it to, which is when it's used within the class under testing (as shown). I don't really understand why it throws a NullReferenceException.