Why does my mock fail?
In the code below, if I understand it correctly, I am stubbing the Speed property and setting it to 0 which should call the Stop method, but when I run the test, it is saying that it expected Stop to be called, but it was not called. Here is the code:
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();
}
}
The actual error I am getting is:
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
You're not calling Stopped()
- so what would be either asking for the Speed
property or calling Stop()
?.
精彩评论