tags:

views:

167

answers:

1

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

+1  A: 

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);
}
sighohwell
Thanks for your help!
Andy