Test if a Presenter is subscribed to an Event?

2 posts, 0 answers
  1. Brendan
    Brendan avatar
    7 posts
    Member since:
    May 2009

    Posted 05 Oct 2012 Link to this post


    I've started using JustMock and I'm wondering how to replicate the example Rhino Mocks sample given here:

    My example is similar to that posted above.
    I have an IView that exposes an Event.
    I have a Presenter class that takes an IView in its constructor and then attaches to the IView Event.

    I want to test that the presenter is actually subscribing to the IView event in its constructor.

    Is there a way to do this with JustMock, like how it's shown in the Rhino Mocks example?


    Edit Update:

    I think I figured it out. Is this correct?

    var view = Mock.Create<IView>();
    Mock.ArrangeSet(() => view.MyEvent += null).IgnoreArguments().OccursOnce();
    var presenter = Mock.Create<Presenter>(view);
  2. Ricky
    Ricky avatar
    467 posts

    Posted 09 Oct 2012 Link to this post

    Hi Brendan,
    Thanks again for contacting us. Currently, you can write the test in the following to verify if the event handler is set properly:

    public class Presenter
        IView view;
        public Presenter(IView view)
            this.view = view;
            this.view.Load += new EventHandler(view_Load);
        void view_Load(object sender, EventArgs e)
            throw new NotImplementedException("Not implemented.");
    public interface IView
        event EventHandler Load;
    public void VerifyAttachesToViewEvents()
        var viewMock = Mock.Create<IView>();
        new Presenter(viewMock);
            Mock.Raise(() => viewMock.Load += null, EventArgs.Empty);
        catch (Exception ex)
            // passes.

    However, your test is a great example itself and I am moving it to our issue tracking system to investigate it further .

    You can follow the progress of the task here:

    Kind Regards
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top