NUnit+Moq 抛出异常
被测试的代码如下。 view.QueryResultsGrid 是一个 System.Windows.Forms.DataGridView 对象:
public void SelectCheckedChanged(object sender, EventArgs e)
{
view.QueryResultsGrid.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
}
正在尝试测试:
private Mock<IQueryForm> mockWindow;
private QueryFormPresenter presenter;
/// <summary>
/// Runs ONCE prior to any tests running
/// </summary>
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
//We're interested in testing the QueryFormPresenter class here, but we
//don't really care about the QueryForm window (view) since there is hardly any code in it.
//Therefore, we create a mock of the QueryForm view, and pass it to the QueryFormPresenter to use.
mockWindow = new Mock<IQueryForm>();
presenter = new QueryFormPresenter(mockWindow.Object);
}
[Test]
public void Moq_Is_Hard()
{
//Arrage
DataGridView d = new DataGridView();
mockWindow.SetupGet(x => x.QueryResultsGrid).Returns(d);
//Act
presenter.SelectCheckedChanged(null, null);
//Assert
//mockView.VerifyGet(x => x.QueryResultsGrid.SelectionMode, Times.AtMostOnce());
mockWindow.VerifySet(x => x.QueryResultsGrid.SelectionMode, Times.AtMostOnce());
}
如果我在测试的代码行上放置断点,VS 告诉我:属性或索引器“Presenter.IQueryForm.QueryResults”不能用于这是因为它缺少 get 访问器。然而,在测试中,我认为我是在模拟上设置 Get 访问器,所以我不理解该消息。最后,NUnit 给出一个:“对象引用未设置到对象的实例”异常。
非常感谢任何帮助!
安迪
The code under test follows. view.QueryResultsGrid is a System.Windows.Forms.DataGridView object:
public void SelectCheckedChanged(object sender, EventArgs e)
{
view.QueryResultsGrid.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
}
Test being attempted:
private Mock<IQueryForm> mockWindow;
private QueryFormPresenter presenter;
/// <summary>
/// Runs ONCE prior to any tests running
/// </summary>
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
//We're interested in testing the QueryFormPresenter class here, but we
//don't really care about the QueryForm window (view) since there is hardly any code in it.
//Therefore, we create a mock of the QueryForm view, and pass it to the QueryFormPresenter to use.
mockWindow = new Mock<IQueryForm>();
presenter = new QueryFormPresenter(mockWindow.Object);
}
[Test]
public void Moq_Is_Hard()
{
//Arrage
DataGridView d = new DataGridView();
mockWindow.SetupGet(x => x.QueryResultsGrid).Returns(d);
//Act
presenter.SelectCheckedChanged(null, null);
//Assert
//mockView.VerifyGet(x => x.QueryResultsGrid.SelectionMode, Times.AtMostOnce());
mockWindow.VerifySet(x => x.QueryResultsGrid.SelectionMode, Times.AtMostOnce());
}
If I put a breakpoint on the line of code under test, VS tells me: The property or indexer 'Presenter.IQueryForm.QueryResults' cannot be used in this context because it lacks the get accessor. However, in the Test, I thought I was setting up the Get accessor on the mock, so I don't understand that message. Finally, NUnit gives an: 'object reference not set to an instance of an object' exception.
Any help is greatly appreciated!
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么您的代码所做的就是从模拟中获取 DataGridView,然后在其上设置 SelectionMode 属性。 DataGridView 的实际实现不受 Mocks 控制,因此它无法拦截 SelectionMode 的 getter 和 setter。
问题之一(正如您可能已经发现的那样)是 DataGridView 并不是真正可 Mockable 的,因此您必须返回实际的实现,删除所有属性并使用它。
下面的测试对我来说通过了......
Well what your code is doing is getting the DataGridView from the mock and then setting the SelectionMode property on it. The actual implementation of DataGridView isn't under Mocks control so it can't intercept the getters and setters for SelectionMode.
One of the problems (as you've probably found out) is that DataGridView isn't really Mockable so you have to return the actual implementation, stub out any properties and make do with that.
The test below passes for me...