为什么当ListView控件中实例化的单元测试都将selectedIndices和SelectedItems无法正常工作? [英] Why do SelectedIndices and SelectedItems not work when ListView is instantiated in unit test?

查看:118
本文介绍了为什么当ListView控件中实例化的单元测试都将selectedIndices和SelectedItems无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在回答自己的问题,因为我找到了解决问题的精神,这个问题,但如果任何人有一个更好的解决方案,我会很乐意听。

I'm writing this question in the spirit of answering your own questions, since I found a solution to the problem, but if anyone has a better solution I would gladly listen to it.

在应用程序,我目前正在我子类ListView控件添加一些功能,其中与ListView将selectedIndices和SelectedItems性能有一定的相互作用。

In the application I am currently working on I am subclassing the ListView control to add some functionality of which some interacts with the ListView SelectedIndices and SelectedItems properties.

现在的问题是,当我试图单元测试我的子类中,将selectedIndices和SelectedItems属性不会当我添加项目到选择更新。我想这两个

The problem is that when I try to unit test my subclass, the SelectedIndices and SelectedItems properties does not update when I add items to the selection. I tried both

item.Selected = true

listView.SelectedIndices.Add(...)

但是将selectedIndices或SelectedItems根本不出现受到影响。单元测试的功能,其他部分正常工作。

But SelectedIndices or SelectedItems simply does not appear to be affected. The unit tests for the other parts of the functionality works fine.

我如何单元测试的选择我的ListView子类的相关部分?

How can I unit test the selection dependent parts of my ListView subclass?

推荐答案

这个问题似乎是在将selectedIndices和SelectedItems不能正常更新,如果在ListView尚未拟定,在此评论从MSDN文档的说明在<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.selected(VS.85).aspx"相对=nofollow> ListViewItem.Selected财产:

The problem seems to be that the SelectedIndices and SelectedItems does not update properly if the ListView has not been drawn, as is stated in this comment from the MSDN documentation of the ListViewItem.Selected property:

Selected属性是不可信任的,如果你的ListView从来没有抽(例如,它在一个TabControl的,在没有被选中的又一个标签)。在这种情况下,父的ListView的SelectedItems和将selectedIndices不正确地更新,将仍然是空的。

The Selected property cannot be trusted if your ListView has never been drawn (for example, it's in a TabControl, in a tab that has not been selected yet). In that case, the SelectedItems and SelectedIndices of the parent ListView are not correctly updated and will still be empty.

一个解决这个问题的方法是在您的测试创建一个简单的哑窗体类,ListView控件添加到窗体,只是展现形式。预期在此之后,将selectedIndices和SelectedItems属性的作用。

One solution to this problem is to create a simple dummy form class in your test, add the ListView to the form and simply show the form. After that the SelectedIndices and SelectedItems properties work as expected.

事情是这样的:

    [Test]
    public void CanGetSelectedItems()
    {
        // simple test to make sure that the SelectedIndices
        // property is updated
        using (var f = new DummyForm(listView))
        {
            f.Show();

            listView.SelectedIndices.Add(0);
            Assert.AreEqual(1, listView.SelectedIndices.Count);
        }
    }

    private class DummyForm : Form
    {
        public DummyForm(ListView listView)
        {
            // Minimize and make it not appear in taskbar to
            // avoid flicker etc when running the tests
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Controls.Add(listView);
        }
    }

这篇关于为什么当ListView控件中实例化的单元测试都将selectedIndices和SelectedItems无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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