Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView没有';通过单元测试调用时不接受数据源_C#_Data Binding_.net 4.0_Datagridview_Datasource - Fatal编程技术网

C# DataGridView没有';通过单元测试调用时不接受数据源

C# DataGridView没有';通过单元测试调用时不接受数据源,c#,data-binding,.net-4.0,datagridview,datasource,C#,Data Binding,.net 4.0,Datagridview,Datasource,该代码在通过UI调用时工作正常,但在通过单元测试调用时不工作。我能够为简单的Winform应用程序重新编程 namespace WinFormApp { public class Pair { public string Key { get; set; } public string Value { get; set; } } public class FormManager { List<Pair&

该代码在通过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
        }
    }
}
以下代码由设计器生成

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对象的某种初始化调用。但在单元测试中使用设计器生成的代码并没有帮助。这与与
表单
对象关联的实际对象有关吗?

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

我看不到您在哪里为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;
    }
List\u source=newlist()
{
新对(){Key=“1”,Value=“one”},
新对(){Key=“2”,Value=“two”}
};
公共表单管理器(DataGridView dgv)
{
dgv.DataSource=_source;
}
单元测试应该没有环境需求,您应该尽可能多地模拟环境需求,以便您的测试集中在一点上。 看看:https://nuget.org/packages/Moq/4.0.10827

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

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

尝试:

[TestClass()]
公共类FormManagerTest
{
[TestMethod()]
public void FormManagerTestSource()
{
var dgv=new System.Windows.Forms.DataGridView();
var_source=新列表()
{
新对(){Key=“1”,Value=“one”},
新对(){Key=“2”,Value=“two”}
};
Assert.AreEqual(2,_source.Count);
//如果要测试dgv行数
dgv.DataSource=_source;
arenequal(2,dataGridView1.Rows.Count);
}
}

添加
dataGridView1.BindingContext=new BindingContext()使这项工作。这个答案有帮助。


@AustinSalonen dataGridView1.Rows.Count为0。这个问题不是关于单元测试和集成测试。这是关于DataGridView在windows form envt之外实例化时无法工作。在上面的代码中,您的测试
FormManagerTestSource
通过了吗?这对我来说是失败的。
 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;
    }
 [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);

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