【问题标题】:DataGridView doesn't accept DataSource when invoked via Unit test通过单元测试调用时,DataGridView 不接受 DataSource
【发布时间】:2012-09-26 18:25:20
【问题描述】:

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

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 对象关联的实际对象有关吗?

【问题讨论】:

  • @AustinSalonen dataGridView1.Rows.Count 是 0。应该是 2。

标签: c# data-binding .net-4.0 datagridview datasource


【解决方案1】:

添加dataGridView1.BindingContext = new BindingContext(); 可以完成这项工作。这个答案有帮助。 Databinding a DataGridView control which is not in Form.Controls collection?

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

【讨论】:

    【解决方案2】:

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

    我没有看到您在哪里设置 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);
    
        }
    }
    

    【讨论】:

    • 这个问题与单元测试与集成测试无关。这是关于 DataGridView 在 Windows 窗体 envt 之外实例化时不起作用。在上面的代码中,你的测试FormManagerTestSource 通过了吗?它对我来说失败了。
    猜你喜欢
    • 2018-09-28
    • 2018-12-02
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多