Using Rhino Mocks 3.6, given the code below I would expect the AssertWasCalled assertion to pass, but it does not. Instead there is the failed assertion message:
"Rhino.Mocks.Exceptions.ExpectationViolationException: IBar.set_Model(7); Expected #1, Actual #0."
Trying IgnoreArguments() does not change the result, but changing the IBar property to a method and asserting the method is called with the argument does work.
What am I missing here?
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
public interface IFoo { }
public interface IBar { int Model { get; set; } }
public class Bar : IBar { public int Model { get; set; } }
public class Foo : IFoo
{
public void MyMethod(IBar bar)
{
bar.Model = 7;
}
}
[TestClass]
public class TestFoo
{
[TestMethod]
public void MyMethod()
{
var foo = new Foo();
var mockBar = MockRepository.GenerateStub<IBar>();
foo.MyMethod(mockBar);
mockBar.AssertWasCalled(b => b.Model = 7);
}
}
If you are stubbing your bar object, then you should make assertion on value of property
Assert.AreEqual(7, mockBar.Name);
If you want to test expectation, you should generate mock instead of stub
var mockBar = MockRepository.GenerateMock<IBar>();
foo.MyMethod(mockBar);
mockBar.AssertWasCalled(b => b.Model = 7);
Difference between stubs and mocks:
A mock is an object that we can set expectations on, and which will verify that the expected actions have indeed occurred. A stub is an object that you use in order to pass to the code under test. You can setup expectations on it, so it would act in certain ways, but those expectations will never be verified. A stub's properties will automatically behave like normal properties, and you can't set expectations on them.
If you want to verify the behavior of the code under test, you will use a mock with the appropriate expectation, and verify that. If you want just to pass a value that may need to act in a certain way, but isn't the focus of this test, you will use a stub.