【C#】DataGridViewのイベントに関する単体試験

■ はじめに

 * NUnitFormでは、DataGridViewのイベントに関する単体試験が現状できなさそうなので、
   代わりにダミークラスでDataGridViewをラップして単体試験を行う

■ サンプル

http://blogs.yahoo.co.jp/dk521123/23687833.html
の単体試験を行う。

コード

[TestFixture]
public class TestClass
{
    [Test]
    public void Test_DataGridView_CellParsing()
    {
        var now = DateTime.Now;
        DummyDataGridView dummyDataGridView =
         new DummyDataGridView("columnName", now.ToString());
        dummyDataGridView.FireOnCellParsingEvent();
        Assert.AreEqual(now.ToString("yyyy/MM/dd"), dummyDataGridView.cellParsingArgs.Value);
    }

    [Test]
    public void Test_DataGridView_CellFormatting()
    {
        var testData = "2001/12/22";
        DummyDataGridView dummyDataGridView =
         new DummyDataGridView("columnName", testData);
        dummyDataGridView.FireOnCellFormattingEvent();
        var ans =
            ((int)(DateTime.Now.Subtract(DateTime.Parse(testData)).TotalDays)/365).ToString();
        Assert.AreEqual(ans, dummyDataGridView.cellFormattingArgs.Value);
    }

    public class DummyDataGridView : DataGridView
    {
        public readonly DataGridViewCellParsingEventArgs cellParsingArgs;
        public readonly DataGridViewCellFormattingEventArgs cellFormattingArgs;

        public DummyDataGridView(string columName, string testValue)
        {
            ExtendDataGridView.Attach(this, columName);
            this.AddColumn(columName);
            this.cellParsingArgs = CreateDataGridViewCellParsingEventArgs(testValue);
            this.cellFormattingArgs = CreateDataGridViewCellFormattingEventArgs(testValue);
        }

        public void FireOnCellParsingEvent()
        {
            this.OnCellParsing(this.cellParsingArgs);
        }

        public void FireOnCellFormattingEvent()
        {
            this.OnCellFormatting(this.cellFormattingArgs);
        }

        private DataGridViewCellParsingEventArgs CreateDataGridViewCellParsingEventArgs(
        string testData)
        {
            int rowIndex = 0;
            int columnIndex = 0;
            Object value = testData;
            Type desiredType = typeof(string);
            DataGridViewCellStyle inheritedCellStyle = new DataGridViewCellStyle();

            return new DataGridViewCellParsingEventArgs(
                rowIndex,
                columnIndex,
                value,
                desiredType,
                inheritedCellStyle);
        }

        private DataGridViewCellFormattingEventArgs CreateDataGridViewCellFormattingEventArgs(
        string testData)
        {
            int rowIndex = 0;
            int columnIndex = 0;
            Object value = testData;
            Type desiredType = typeof(string);
            DataGridViewCellStyle inheritedCellStyle = new DataGridViewCellStyle();

            return new DataGridViewCellFormattingEventArgs(
                rowIndex,
                columnIndex,
                value,
                desiredType,
                inheritedCellStyle);
        }

        private void AddColumn(string columnName)
        {
            DataGridViewTextBoxColumn tsetColumn = new DataGridViewTextBoxColumn();
            tsetColumn.DataPropertyName = columnName;
            tsetColumn.Name = columnName;
            this.Columns.Add(tsetColumn);
        }
    }
}

関連記事

C#】 NUnitForms ~ あれこれ編 ~

https://blogs.yahoo.co.jp/dk521123/21717521.html