This is a migrated thread and some comments may be shown as answers.

Test if a Presenter is subscribed to an Event?

1 Answer 152 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brendan
Top achievements
Rank 1
Brendan asked on 05 Oct 2012, 05:13 PM
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);

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 09 Oct 2012, 06:27 PM
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.

Tags
General Discussions
Asked by
Brendan
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or