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