Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying event registration using Moq

I'm developing an asp.net (classic) application trying to implement the MVP pattern using this example. In trying to unit test my presenter and using the following pattern, the psuedocode for which looks like so

//base view interface public interface IView {     event EventHandler Init;      event EventHandler Load;      bool IsPostBack { get; }      void DataBind();      bool IsValid { get;} }  //presenter psuedo code public class SomePresenter {      public SomePresenter(ISomeDomainService service, IView someView)      {            ...            //HOW DO WE TEST/VERIFY THAT THIS REGISTRATION OCCURS?            someView.Init += OnInit;            someView.Load += OnLoad;      } } ... //consuming code that exercises the above code, that needs to be tested var presenter = new SomePresenter(someDomainService, someView); 

How do I verify that the presenter is doing what is expected i.e. registering for the Init and Load events? While this is easily done in the Phil Haack's example using Rhino mocks...

[Test] public void VerifyAttachesToViewEvents() {     viewMock.Load += null;     LastCall.IgnoreArguments();     viewMock.PostSaved += null;     LastCall.IgnoreArguments();     mocks.ReplayAll();     new PostEditController(viewMock,        this.dataServiceMock);     mocks.VerifyAll(); } 

... how can we do this using MOQ?

like image 616
Dilip Krishnan Avatar asked Sep 15 '09 20:09

Dilip Krishnan


People also ask

What does Moq verify do?

Verifies that all verifiable expectations have been met.

Is Moq a testing framework?

The Moq framework is an open source unit testing framework that works very well with .

What can be mocked with Moq?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces. However, there are a few limitations you should be aware of. The classes to be mocked can't be static or sealed, and the method being mocked should be marked as virtual.

What is Moq mocking framework?

Moq is a mocking framework built to facilitate the testing of components with dependencies. As shown earlier, dealing with dependencies could be cumbersome because it requires the creation of test doubles like fakes. Moq makes the creation of fakes redundant by using dynamically generated types.


2 Answers

The moq 4.13 introduced this feature. Now it is possible to verify if add\remove has been invoked. Therefore four new methods have been introduced:

  1. SetupAdd
  2. SetupRemove
  3. VerifyAdd
  4. VerifyRemove

Example

var mock = new Mock<IAdder<EventArgs>>(); mock.SetupAdd(m => m.Added += (sender, args) => { });  mock.Object.Added += (sender, args) => { }; mock.Object.Added += (sender, args) => { };  mock.VerifyAdd(m => m.Added += It.IsAny<EventHandler>(), Times.Exactly(2)); 

NB: Notice that in order to verify at least one setup should be added. The reason is to keep backward compatibility with the older version of moq.

like image 146
Johnny Avatar answered Oct 03 '22 18:10

Johnny


It would appear that this functionality is not currently available in moq, but may appear in a future version (I had a look in the 4.0.812.4 beta, but it doesn't seem to be there).

It may be worth asking the question, "why does SomePresenter need to subscribe to the View's Load and Init events?" Presumably it is because the SomePresenter class needs to respond to those events. So it might be better to use the Raise method on your Mock<IView> to raise the Load and Init events, and then assert that SomePresenter did the right thing in response to them.

like image 36
Mark Heath Avatar answered Oct 03 '22 18:10

Mark Heath