Nmock2 和事件期望
我正在为一个遵循 MVP 模式的小型应用程序编写测试。
从技术上讲,我知道我应该在代码之前编写测试,但我需要快速智能地构建一个演示应用程序,所以我现在回到测试,然后再进行真正的开发。
简而言之,我正在尝试测试演示者,但是由于Internal.ExpectationException,我什至无法运行空测试。
意外调用事件分配时会引发异常。
这是演示者类,
public LBCPresenter(IView view, IModel model)
{
m_model = model;
m_model.BatteryModifiedEvent += new EventHandler(m_model_BatteryModifiedEvent);
}
模型接口
public interface IModel
{
event EventHandler BatteryModifiedEvent;
}
,这是测试类,我看不出我缺少什么,我告诉 NMock 期待该事件......
[TestFixture]
public class MVP_PresenterTester
{
private Mockery mocks;
private IView _mockView;
private IViewObserver _Presenter;
private IModel _mockModel;
[SetUp]
public void SetUp()
{
mocks = new Mockery();
_mockView = mocks.NewMock<IView>();
_mockModel = mocks.NewMock<IModel>();
_Presenter = new LBCPresenter(_mockView, _mockModel);
}
[Test]
public void TestClosingFormWhenNotDirty()
{
Expect.Once.On(_mockModel).EventAdd("BatteryModifiedEvent", NMock2.Is.Anything);
//makes no difference if following line is commented out or not
//mocks.VerifyAllExpectationsHaveBeenMet();
}
}
每次我运行测试时,我都会得到相同的期望异常。
有什么想法吗?
Im in the process of writing a test for a small application that follows the MVP pattern.
Technically, I know I should have written the test before the code, but I needed to knock up a demo app quick smart and so im now going back to the test before moving on to the real development.
In short I am attempting to test the presenter, however I cannot even get an empty test to run due to an Internal.ExpectationException.
The exception is raised on a unexpected invocation of an event assignation.
Here is the presenter class,
public LBCPresenter(IView view, IModel model)
{
m_model = model;
m_model.BatteryModifiedEvent += new EventHandler(m_model_BatteryModifiedEvent);
}
Model Interface
public interface IModel
{
event EventHandler BatteryModifiedEvent;
}
And here is the test class, I can't see what im missing, ive told NMock to expect the event...
[TestFixture]
public class MVP_PresenterTester
{
private Mockery mocks;
private IView _mockView;
private IViewObserver _Presenter;
private IModel _mockModel;
[SetUp]
public void SetUp()
{
mocks = new Mockery();
_mockView = mocks.NewMock<IView>();
_mockModel = mocks.NewMock<IModel>();
_Presenter = new LBCPresenter(_mockView, _mockModel);
}
[Test]
public void TestClosingFormWhenNotDirty()
{
Expect.Once.On(_mockModel).EventAdd("BatteryModifiedEvent", NMock2.Is.Anything);
//makes no difference if following line is commented out or not
//mocks.VerifyAllExpectationsHaveBeenMet();
}
}
Every time I run the test I get the same expectation Exception.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是一个计时问题 - 您在测试设置中调用演示者构造函数 - 这意味着事件添加是在您的测试设置 EventAdd 期望之前发生的。
如果将调用移动到 EventAdd 期望下方的演示者构造函数,它应该可以工作
I think its a timing issue - you are calling the presenter constructor in the test Setup - this means the event add is happening before your test sets up the EventAdd expectation.
If you move your call to the presenter constructor below the EventAdd expection it should work