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

    Hi,

    I've started using JustMock and I'm wondering how to replicate the example Rhino Mocks sample given here:
    http://stackoverflow.com/questions/9364254/verifying-event-handler-exists-in-moq-vs-rhino-mocks

    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?

    Thanks.

    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);
     
    Mock.Assert(view);
  2. Ricky
    Admin
    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;
    }
     
    [TestMethod]
    public void VerifyAttachesToViewEvents()
    {
        var viewMock = Mock.Create<IView>();
        
        new Presenter(viewMock);
     
        try
        {
            Mock.Raise(() => viewMock.Load += null, EventArgs.Empty);
            Assert.Fail();
        }
        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:
    http://www.telerik.com/support/pits.aspx#/public/justmock/12895

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
Back to Top