views:

678

answers:

3

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 Initilization 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();
});
        }
A: 
  1. What exactly are you trying to test in the Test method?
  2. You should try to avoid using strict mocks.
  3. I suggest using the Rhino's AAA syntax (Arrange, Act, Assert).
Igor Brejc
A: 

The problem lied with me not understanding the record/verify that is going on with Strict mocks. In order to fix the issue I was having this is how I changed my TestInitilize function. This basicaly does a quick test on my intial state I'm setting up for all my tests.

[TestInitialize]
    public void Initilize()
    {
        _mocks = new MockRepository();
        _view = _mocks.StrictMock<IView>();
        _presenter = new Presenter(_view);

        Expect.Call(delegate { _presenter.View.InvokedMethod(); });
        _mocks.ReplayAll();
        _mocks.VerifyAll();
        _mocks.BackToRecordAll();
        _presenter.Property = 1;
    }
Again, are you sure you need strict mocking? Your scenario looks more like using an ordinary mock which does not force you to specify all of the expectations. And if you use AAA, it's much simpler to code it.
Igor Brejc
+1  A: 

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

Rune Sundling