7

该代码在通过 UI 调用时工作正常,但在通过单元测试调用时不起作用。我能够为简单的 Winform App 重现这个。

namespace WinFormApp
{
    public class Pair
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }

    public class FormManager
    {
        List<Pair> _source = new List<Pair>()
        {
            new Pair() { Key="1", Value = "one" },
            new Pair() { Key = "2", Value = "two" }
        };

        public FormManager(DataGridView dgv)
        {
            dgv.DataSource = _source;
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            FormManager manager = new FormManager(dataGridView1); // This works
        }
    }
}

单元测试代码

namespace WinFormApp.Test
{
    [TestClass()]
    public class FormManagerTest
    {
        private DataGridView dataGridView1;

        [TestMethod()]
        public void FormManagerTestSource()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();

            FormManager target = new FormManager(dataGridView1);

            Assert.AreEqual(2, dataGridView1.Rows.Count); // This fails.
        }
    }
}

以下代码由设计师生成

private void InitializeComponent()
{
    this.dataGridView1 = new System.Windows.Forms.DataGridView();
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGridView1
    // 
    this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.dataGridView1.Location = new System.Drawing.Point(20, 27);
    this.dataGridView1.Name = "dataGridView1";
    this.dataGridView1.Size = new System.Drawing.Size(240, 150);
    this.dataGridView1.TabIndex = 0;
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 262);
    this.Controls.Add(this.dataGridView1);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
    this.ResumeLayout(false);

}

我的猜测是我在单元测试代码路径中缺少对 dataGridView1 对象的某种初始化调用。但是在单元测试中使用设计器生成的代码并没有帮助。这与与对象关联的实际对象有关Form吗?

4

2 回答 2

9

添加dataGridView1.BindingContext = new BindingContext();使这项工作。这个答案有帮助。 对不在 Form.Controls 集合中的 DataGridView 控件进行数据绑定?

[TestMethod()]
public void FormManagerTestSource()
{
    this.dataGridView1 = new System.Windows.Forms.DataGridView();
    FormManager target = new FormManager(dataGridView1);
    Assert.AreEqual(0, dataGridView1.Rows.Count); // 0 initially.
    dataGridView1.BindingContext = new BindingContext(); // this makes it work.
    Assert.AreEqual(2, dataGridView1.Rows.Count); // 2 as expected.
}
于 2012-10-08T09:02:42.977 回答
0

看起来您正在尝试实现集成测试而不是单元测试

我看不到您在哪里设置 dataGridView1 的数据源

在您的 WinFormApp 中,您将数据源设置为:

 List<Pair> _source = new List<Pair>()
    {
        new Pair() { Key="1", Value = "one" },
        new Pair() { Key = "2", Value = "two" }
    };

    public FormManager(DataGridView dgv)
    {
        dgv.DataSource = _source;
    }

单元测试应该没有环境要求,你应该尽可能多地模拟,这样你的测试就集中在一个点上。看看:https://nuget.org/packages/Moq/4.0.10827

为每个组件或单元创建单独的测试

我看到您正在尝试验证行数

尝试:

 [TestClass()]
public class FormManagerTest
{


    [TestMethod()]
    public void FormManagerTestSource()
    {
        var dgv = new System.Windows.Forms.DataGridView();
        var _source = new List<Pair>()
    {
        new Pair() { Key="1", Value = "one" },
        new Pair() { Key = "2", Value = "two" }
    };
        Assert.AreEqual(2, _source.Count); 
        //If you want to test dgv row count
        dgv.DataSource = _source;
        Assert.AreEqual(2, dataGridView1.Rows.Count);

    }
}
于 2012-10-05T15:48:15.423 回答