使用Moq验证事件注册 [英] Verifying event registration using Moq
问题描述
我正在开发一个asp.net(经典)应用程序,以尝试实施MVP模式
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);
如何验证演示者是否正在按预期进行操作,即注册Init和Load事件?尽管在 Phil Haack的示例中使用Rhino模拟就可以轻松完成此操作...
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?
推荐答案
该功能似乎是暂时不可用,但可能会在以后的版本中出现(我曾在4.0.812.4 beta中看到过它,但似乎不存在).
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).
也许值得问一个问题,为什么SomePresenter
需要订阅View的Load
和Init
事件?"大概是因为SomePresenter
类需要响应那些事件.因此,最好在Mock<IView>
上使用Raise
方法来引发Load
和Init
事件,然后断言SomePresenter
对它们做出了正确的事情.
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.
这篇关于使用Moq验证事件注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!