NUnit+Moq 抛出异常

发布于 2024-08-02 11:40:25 字数 1499 浏览 1 评论 0原文

被测试的代码如下。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮色兮凉城 2024-08-09 11:40:25

那么您的代码所做的就是从模拟中获取 DataGridView,然后在其上设置 SelectionMode 属性。 DataGridView 的实际实现不受 Mocks 控制,因此它无法拦截 SelectionMode 的 getter 和 setter。

问题之一(正如您可能已经发现的那样)是 DataGridView 并不是真正可 Mockable 的,因此您必须返回实际的实现,删除所有属性并使用它。

下面的测试对我来说通过了......

[Test]
public void SelectCheckedChanged_SetsModeToColumnHeaderSelect () {
    //Arrange
    //Mock the form to return a stubbed grid
    Mock<IQueryForm> form = new Mock<IQueryForm>();
    DataGridView grid = new DataGridView();
    grid.SelectionMode = DataGridViewSelectionMode.CellSelect;
    form.SetupGet(f => f.QueryResults).Returns(grid);
    QueryFormPresenter presenter = new QueryFormPresenter(form.Object);

    //Act
    presenter.SelectCheckedChanged();

    //Assert
    form.VerifyGet(f => f.QueryResults);
    Assert.AreEqual(DataGridViewSelectionMode.ColumnHeaderSelect, 
                    grid.SelectionMode);
}

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...

[Test]
public void SelectCheckedChanged_SetsModeToColumnHeaderSelect () {
    //Arrange
    //Mock the form to return a stubbed grid
    Mock<IQueryForm> form = new Mock<IQueryForm>();
    DataGridView grid = new DataGridView();
    grid.SelectionMode = DataGridViewSelectionMode.CellSelect;
    form.SetupGet(f => f.QueryResults).Returns(grid);
    QueryFormPresenter presenter = new QueryFormPresenter(form.Object);

    //Act
    presenter.SelectCheckedChanged();

    //Assert
    form.VerifyGet(f => f.QueryResults);
    Assert.AreEqual(DataGridViewSelectionMode.ColumnHeaderSelect, 
                    grid.SelectionMode);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文