Moq - mock.Raise should raise event in tested unit without having a Setup

Yannik picture Yannik · May 16, 2011 · Viewed 39.7k times · Source

I have a presenter class, that attaches an event of the injected view. Now I would like to test the presenter reacting on correctly on the event.

This is the view interface IView:

public interface IView 
{
    event EventHandler MyEvent;
    void UpdateView(string test);
}

This ist the view implementing IView

public partial class MyView : IView
{
    public event EventHandler MyEvent;

    public MyView()
    {
        this.combo.SelectedIndexChanged += this.OnSelectedIndexChanged;
    }

    public void UpdateView(string test)
    {
        this.textBox.Text = test;
    }

    private OnSelectedIndexChanged(Object sender, EventArgs e)
    {
        if (this.MyEvent != null)
        {
            this.MyEvent(sender, e);
        }
    }
}

This ist the presenter under test:

public class MyPresenter
{
    private IView _view;
    public MyPresenter(IView view)
    {
        this._view = view;
        this._view.MyEvent += this.OnMyEvent;
    }

    private void OnMyEvent(Object sender, EventArgs e)
    {
        this._view.UpdateView();
    }
}

This is the test fixture testing MyPresenter:

[TestClass]
public class MyPresenterFixture()
{
    private MyPresenter testee;
    private Mock<IView> mockView;

    [TestMethod]
    public void ShouldReactOnMyEvent()
    {
        // arrange
        this.mockView = new Mock<IView>(MockBehavior.Strict);
        this.testee = new MyPresenter(this.mockView.Object);

        // act
        this.mockView.Raise(mock => mock.MyEvent += null); // this does not fire

        // assert and verify
        this.mockView.Verify(mock => mock.UpdateView(It.IsAny<string>());
    }
}

I am using Moq 4. Is it possible to do what I want?

Best regards Yannik

Answer

Anderson Imes picture Anderson Imes · May 18, 2011

Don't you need to pass the argument? Your event signature is EventHandler, which is
(object sender, EventArgs e).

this.mockView.Raise(mock => mock.MyEvent += null, new EventArgs());

I've never used the overload you've specified here... it doesn't seem correct, though.