测试源自ComboBox的自定义控件 [英] Testing Custom Control derived from ComboBox

查看:105
本文介绍了测试源自ComboBox的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个从ComboBox派生的控件,希望对它的行为进行单元测试。

I've created a control derived from ComboBox, and wish to unit test its behaviour.

但是,在我的单元测试中,它的行为方式似乎有所不同

However, it appears to be behaving differently in my unit test to how it behaves in the real application.

在实际应用程序中,Combobox.DataSource属性和.Items同步-换句话说,当我更改Combobox.DataSource时,项目列表会立即自动更新,以显示数据源中每个元素的项目。

In the real application, the Combobox.DataSource property and the .Items sync up - in other words when I change the Combobox.DataSource the .Items list immediately and automatically updates to show an item for each element of the DataSource.

在我的测试中,我构造了一个ComboBox,为其分配了数据源,但是.Items列表完全不更新,仅保留0个项目。因此,当我尝试在测试中将.SelectedIndex更新为0以选择第一个项目时,我收到了ArgumentOutOfRangeException。

In my test, I construct a ComboBox, assign a datasource to it, but the .Items list doesn't get updated at all, remaining at 0 items. Thus, when I try to update the .SelectedIndex to 0 in the test to select the first item, I recieve an ArgumentOutOfRangeException.

这是因为我没有Application.Run在我的单元测试中运行以启动事件循环,或者这有点像红鲱鱼?

Is this because I don't have an Application.Run in my unit test starting an event loop, or is this a bit of a red herring?

编辑:第一个测试的更多细节:

More detail on the first test:

    [SetUp]
    public void SetUp()
    {
        mECB = new EnhancedComboBox();

        mECB.FormattingEnabled = true;
        mECB.Location = new System.Drawing.Point( 45, 4 );
        mECB.Name = "cboFind";
        mECB.Size = new System.Drawing.Size( 121, 21 );
        mECB.TabIndex = 3;

        mECB.AddObserver( this );

        mTestItems = new List<TestItem>();
        mTestItems.Add( new TestItem() { Value = "Billy" } );
        mTestItems.Add( new TestItem() { Value = "Bob" } );
        mTestItems.Add( new TestItem() { Value = "Blues" } );

        mECB.DataSource = mTestItems;
        mECB.Reset();

        mObservedValue = null;
    }

    [Test]
    public void Test01_UpdateObserver()
    {
        mECB.SelectedIndex = 0;
        Assert.AreEqual( "Billy", mObservedValue.Value );
    }

当尝试将SelectedIndex设置为0时,测试在第一行失败。在调试时,这似乎是因为更改.DataSource时,不会更新.Items集合来反映这一点。但是,在调试实际应用程序时,.DataSource更改时,.Items集合始终会更新。

The test fails on the first line, when trying to set the SelectedIndex to 0. On debugging, this appears to be because when the .DataSource is changed, the .Items collection is not updated to reflect this. However, on debugging the real application, the .Items collection is always updated when the .DataSource changes.

当然,我不必在测试中实际渲染ComboBox。 ,我什至没有设置任何要渲染的绘图表面!也许我唯一需要的答案是在我实际上不需要绘制框的单元测试场景中,如何以与绘制组合框相同的方式进行更新?

Surely I don't have to actually render the ComboBox in the test, I don't even have any drawing surfaces set up to render on to! Maybe the only answer I need is "How do I make the ComboBox update in the same way as when it is drawn, in a unit test scenario where I don't actually need to draw the box?"

推荐答案

由于您只是在调用构造函数,因此组合框的许多功能将无法使用。例如,当在屏幕上的窗体上绘制ComboBox时,将填充项目。在单元测试中构建时不会发生这种情况。

Since you're simply calling the constructor, a lot of functionality of the combobox will not work. For example, the items will be filled when the ComboBox is drawn on screen, on a form. This does not happen when constructing it in a unit test.

为什么要在该组合框中编写单元测试?

Why do you want to write a unit test on that combobox?

您不能分隔定制控件中的逻辑吗?例如,将其放在控制器中并进行测试?

Can't you seperate the logic which now is in the custom control? For example put this in a controller, and test that?

为什么不对DataSource属性而不是Items集合进行测试?

Why don't you test on the DataSource property instead of the Items collection?

这篇关于测试源自ComboBox的自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆