在下面的代碼中,如果我理解正確,我將Speed屬性存根並將其設置爲0,這應該調用Stop方法,但是當我運行測試時,它表示它期望Stop被調用,但是它沒有被叫。下面是代碼:爲什麼我的模擬失敗?
public class Car
{
public virtual int Speed { get; set; }
public virtual bool Stopped()
{
if (Speed > 0)
return false;
Stop();
return true;
}
public virtual void Stop()
{
}
}
[TestFixture]
public class CarTests
{
[Test]
public void WhenSpeedIsZeroCarShouldBeStopped()
{
var carMock = MockRepository.GenerateMock<Car>();
carMock.Stub(x => x.Speed).Return(0);
carMock.Expect(x => x.Stop());
carMock.VerifyAllExpectations();
}
}
我得到實際的錯誤是:
Rhino.Mocks.Exceptions.ExpectationViolationException: Car.Stop(); Expected #1, Actual #0.
at Rhino.Mocks.Impl.ReplayMockState.Verify()
at Rhino.Mocks.MockRepository.Verify(Object obj)
at Rhino.Mocks.RhinoMocksExtensions.VerifyAllExpectations(Object mockObject)
at MockTutorial.CarTests.WhenSpeedIsZeroCarShouldBeStopped() in C:\Programming\Test\MockTutorial\MockTutorial\DirectoryInfoSample.cs:line 94
你實際上並沒有在車上做點什麼。所以你怎麼能期望Stop()被調用? – extraneon 2010-09-08 19:23:55
這是一個有點混亂,因爲我是嘲笑新手。 – Xaisoft 2010-09-08 19:26:39