Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView doesn't accept DataSource when invoked via Unit test

The code works fine when invoked via UI but doesn't work when called via unit test. I was able to repro this for simple 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
        }
    }
}

Unit test code

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

Following code was generated by designer

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);

}

My guess is I'm missing some kind of init call on dataGridView1 object in unit test code path. But using designer generated code in unit test didn't help. Has this to do with actual object associated with a Form object?

like image 401
Ankush Avatar asked Oct 05 '12 14:10

Ankush


1 Answers

Adding dataGridView1.BindingContext = new BindingContext(); makes this work. This answer helped. 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.
}
like image 64
Ankush Avatar answered Sep 23 '22 09:09

Ankush