R – Using Rhino Mocks, why does invoking a mocked on a property during test initialization return Expected call #1, Actual call #0

rhinorhino-mocks

I currently have a test which tests the presenter I have in the MVP model. On my presenter I have a property which will call into my View, which in my test is mocked out. In the Initialization of my test, after I set my View on the Presenter to be the mocked View, I set my property on the Presenter which will call this method.

In my test I do not have an Expect.Call for the method I invoke, yet when I run I get this Rhino mock exception:

Rhino.Mocks.Exceptions.ExpectationViolationException: IView.MethodToInvoke(); Expected #1, Actual #0..

From what I understand with Rhino mocks, as long as I am invoking on the Mock outside the expecting block it should not be recording this. I would imagine the test to pass. Is there a reason it is not passing?

Below is some code to show my setup.

public class Presenter
{
    public IView View;

    public Presenter(IView view)
    {
        View = view
    }
    private int _property;
    public int Property
    get { return _property;}
    set
    {
       _property = value;

       View.MethodToInvoke();
    }
}

… Test Code Below …

[TestInitialize]
        public void Initilize()
        {
            _mocks = new MockRepository();
            _view = _mocks.StrictMock<IView>();
            _presenter = new Presenter(_view);
            _presenter.Property = 1;            
        }
[TestMethod]
        public void Test()
        {
            Rhino.Mocks.With.Mocks(_mocks).Expecting(delegate
            {                
            }).Verify(delegate
            {
                _presenter.SomeOtherMethod();
});
        }

Best Answer

Why in the world would you want to test the same thing each time a test is run?

If you want to test that a specific thing happens, you should check that in a single test.

The pattern you are using now implies that you need to - set up prerequisites for testing - do behavior - check that behavior is correct and then repeat that several times in one test

You need to start testing one thing for each test, and that help make the tests clearer, and make it easier to use the AAA syntax.

There's several things to discuss here, but it certainly would be clearer if you did it something like:

[TestMethod]
ShouldCallInvokedMethodWhenSettingProperty()
{
   var viewMock = MockRepository.GenerateMock<IView>()

   var presenter = new Presenter(viewMock);

   presenter.Property = 1;

   viewMock.AssertWasCalled(view => view.InvokedMethod());

}

Read up more on Rhino Mocks 3.5 syntax here: http://ayende.com/Wiki/Rhino+Mocks+3.5.ashx

Related Topic