views:

138

answers:

1

Im in the process of writing a test for a small application that follows the MVP pattern.

Technically, I know I should have written the test before the code, but I needed to knock up a demo app quick smart and so im now going back to the test before moving on to the real development.

In short I am attempting to test the presenter, however I cannot even get an empty test to run due to an Internal.ExpectationException.

The exception is raised on a unexpected invocation of an event assignation.

Here is the presenter class,

   public LBCPresenter(IView view, IModel model)
   {
        m_model = model;

        m_model.BatteryModifiedEvent += new EventHandler(m_model_BatteryModifiedEvent);
   }

Model Interface

    public interface IModel
    {
         event EventHandler BatteryModifiedEvent;
    }

And here is the test class, I can't see what im missing, ive told NMock to expect the event...

    [TestFixture]
public class MVP_PresenterTester
{
    private Mockery mocks;
    private IView _mockView;
    private IViewObserver _Presenter;
    private IModel _mockModel;

    [SetUp]
    public void SetUp()
    {
        mocks = new Mockery();

        _mockView = mocks.NewMock<IView>();
        _mockModel = mocks.NewMock<IModel>();
        _Presenter = new LBCPresenter(_mockView, _mockModel);

    }

    [Test]
    public void TestClosingFormWhenNotDirty()
    {
         Expect.Once.On(_mockModel).EventAdd("BatteryModifiedEvent", NMock2.Is.Anything);

       //makes no difference if following line is commented out or not
       //mocks.VerifyAllExpectationsHaveBeenMet();
    }
}

Every time I run the test I get the same expectation Exception.

Any ideas?

+1  A: 

I think its a timing issue - you are calling the presenter constructor in the test Setup - this means the event add is happening before your test sets up the EventAdd expectation.

If you move your call to the presenter constructor below the EventAdd expection it should work

Richard