0
下面是测试中的代码。 view.QueryResultsGrid是System.Windows.Forms.DataGridView对象:正在尝试NUnit + Moq抛出异常
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给出:'未设置对象实例的对象引用'异常。
任何帮助,非常感谢!
安迪
感谢您的帮助! – Andy 2009-08-14 21:41:12