验证 Moq 与 Rhino Mocks 中是否存在事件处理程序
我希望能够测试对象是否已订阅事件。在Rhino Mocks中,下面的代码验证了这一点,但是我找不到在Moq中做到这一点的方法。
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 Exception("Not implemented.");
}
}
[Test]
public void VerifyAttachesToViewEvents()
{
MockRepository mocks = new MockRepository();
IView viewMock = (IView)mocks.CreateMock(typeof(IView));
using (mocks.Record())
{
viewMock.Load += null;
LastCall.IgnoreArguments();
}
new Presenter(viewMock);
mocks.VerifyAll();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Moq http://code.google 中似乎缺少一个错误/功能.com/p/moq/issues/detail?id=100
Looks like its a bug/feature missing in Moq http://code.google.com/p/moq/issues/detail?id=100
我会引发该事件并验证是否引发了异常。无论如何,您可能需要检查 SUT 中的某些逻辑是否已执行,而不仅仅是订阅了事件:
我不确定您使用的是什么单元测试框架,因此利用了 断言部分的流畅断言。如果您使用 NUnit,代码可能是:
I would raise the event and verify whether an exception has been thrown. Anyway you probably need to check if some logic in your SUT is executed not just that an event has been subscribed to:
I was not sure what unit testing framework you use, so leveraged fluent assertions in the assert part. If you use NUnit the code may be: