Search code examples
c#.netunit-testingeventsrhino-mocks

Rhino Mocks. How to add expectation that event handler was subscribed


I have an interface like that:

interface IView
{
     event EventHandler<MyEventArgs> SomeEvent;
}

and a class

class Presenter
{
     private IView _view;
     public Presenter(IView view)
     {
         view.SomeEvent += MyEventHandler;
     }

     private MyEventHandler(...)
}

I'm trying to test this stuff using RhinoMocks and MockRepository.VerifyAll() throws the following exception

Rhino.Mocks.Exceptions.ExpectationViolationException: IView.add_SomeEvent(System.EventHandler`1[MyEventArgs]); Expected #1, Actual #0.

So the question:

How to add the expectation that event is subscribed?


Solution

  • Sorry guys, I have found what I was doing wrong:

    _viewMock.Expect(x => x.SomeEvent+= Arg<EventHandler<MyEventArgs>>.Is.Anything); 
    
    Presenter p = new Presenter(_viewMock);
    
    _mockRepository.ReplayAll();
    
    ...
    
    _mockRepository.VerifyAll();
    

    I had to ReplayAll before I created new instance of Presenter.

    Thanks.