Rhino Mocks: How do I make fun of a method call in a method call?

I have a really simple class with two methods; One that will be called, and another that he will call. The idea is to call the OuterMockMethod method, but mock InnerMockMethod. Right now, I can only make fun of 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 this is not the method I want to make fun of:

  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 OuterMockMethod, which he likes, but I don't want that. I want to mock InnerMockMethod so that when I call OuterMockMethod, it returns what I want.

  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"); } 
+4
source share
1 answer

In this case, you need to put mock on the returned object:

 MockClass returnedMock = MockRepository.GenerateMock<MockClass>(); returnedMock.Expect( rm => rm.InnerMockMethod() ) .Return( new MockClass { SomeMessage = "Mocked" } ); mockProvider.Expect( mp => mp.OuterMockMethod() ).Return (returnedMock ); returnedClass = mockProvider.OuterMockMethod(); ... 

Please note that StrictMock is deprecated. A preferred example is AAA (Arrange, Act, Assert). You can find more information here .

+7
source

All Articles