I have a really simple class with two methods; One that will be called and the other that it will call. The idea is to call the OuterMockMethod method BUT mock the InnerMockMethod. Right now I can only seem to mock the OuterMockMethod method.
public class MockClass : IMockInterface { public virtual MockClass InnerMockMethod() { MockClass returnValue; returnValue = new MockClass(); returnValue.SomeMessage = 'Not mocked'; return returnValue; } public virtual MockClass OuterMockMethod() { MockClass mock; mock = new MockClass(); return mock.MockedMethod(); } }
Now this works, but it isn’t the method I want to mock:
public void MockTest_Internal() { MockClass returnedClass; MockClass mockProvider; mockProvider = repository.StrictMock<MockClass>(); mockProvider.Expect(item => item.OuterMockMethod()) .Return(new MockClass { SomeMessage = 'Mocked' }); repository.Replay(mockProvider); returnedClass = mockProvider.OuterMockMethod(); Assert.IsTrue(returnedClass.SomeMessage == 'Mocked'); }
As you can see, it calls the OuterMockMethod which it likes but I don’t want that. I want to mock the InnerMockMethod so that when it’s called by the OuterMockMethod it will return what I want it to.
public void MockTest_Internal() { MockClass returnedClass; MockClass mockProvider; mockProvider = repository.StrictMock<MockClass>(); mockProvider.Expect(item => item.InnerMockMethod()) .Return(new MockClass { SomeMessage = 'Mocked' }); repository.Replay(mockProvider); returnedClass = mockProvider.OuterMockMethod(); //Boom angry Rhino Assert.IsTrue(returnedClass.SomeMessage == 'Mocked'); }
In this case you need to put the mock on the returned object:
Note that StrictMock has been deprecated. The preferred pattern is now AAA (Arrange, Act, Assert). You can find more info here.