I am playing around with MbUnit and Rhino Mocks and made a simple test. It may be poorly designed code, but I am more focused on just seeing if I can get the test to pass. Basically, When the engine light of a car is on, the car should an oil change. Here is code:
public interface ICar
{
bool EngineLight { get; set; }
void GetOilChange();
bool CheckEngineLight(ICar car);
}
public class Car : ICar
{
public bool EngineLight { get; set; }
public void GetOilChange()
{
}
public bool CheckEngineLight(ICar car)
{
if (car.EngineLight)
GetOilChange();
return true;
return false;
}
}
[TestFixture]
public class CarTests
{
[Test]
public void WhenEngineLightIsOnGetOilChange()
{
var carMock = MockRepository.GenerateMock<ICar>();
carMock.Stub(x => x.EngineLight).Return(true);
Assert.AreEqual(true, new Car().CheckEngineLight(carMock)); //This passes
carMock.AssertWasCalled(x => x.GetOilChange()); //This fails
}
}
First off, this code has a bug. Add in the {} around the IF:
The reason it fails is because you are calling new Car().CheckEngineLight … and the new Car() is calling GetOilChange … the carMock is not calling GetOilChange. Change the code to
car.GetOilChange()(see it in the code above).The fact that you are passing a Car object into a method on the Car Object is very confusing. Why not change the code to this:
Change your test to: