4
我有一個非常簡單的類有兩種方法;一個將被調用,另一個將調用。這個想法是調用OuterMockMethod方法,但模擬InnerMockMethod。現在我只能模仿OuterMockMethod方法。Rhino Mocks:如何在方法調用中模擬方法調用?
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();
}
}
現在這個工作,但它不是我想要嘲笑方法:
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");
}
正如你所看到的,它會調用OuterMockMethod它喜歡,但我不希望出現這種情況。我想嘲笑InnerMockMethod,這樣當它被OuterMockMethod調用時,它將返回我想要的內容。
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");
}
再次感謝。非常有幫助。 – 2008-12-15 23:35:09