简体   繁体   English

为什么在单元测试中实例化ListView时SelectedIndices和SelectedItems不起作用?

[英]Why do SelectedIndices and SelectedItems not work when ListView is instantiated in unit test?

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. 我正在回答你自己的问题时写这个问题,因为我找到了问题的解决方案,但如果有人有更好的解决方案,我很乐意听。

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. 在我正在处理的应用程序中,我正在继承ListView控件以添加一些功能,其中一些功能与ListView 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. 问题是,当我尝试对子类进行单元测试时,当我向选择中添加项时,SelectedIndices和SelectedItems属性不会更新。 I tried both 我试过了两个

item.Selected = true

and

listView.SelectedIndices.Add(...)

But SelectedIndices or SelectedItems simply does not appear to be affected. 但SelectedIndices或SelectedItems似乎并未受到影响。 The unit tests for the other parts of the functionality works fine. 单元测试功能的其他部分工作正常。

How can I unit test the selection dependent parts of my ListView subclass? 如何对ListView子类的选择依赖部分进行单元测试?

I found a trick you can use to get those properties populated: 我发现了一个可以用来填充这些属性的技巧:

listView.AccessibilityObject.ToString(); listView.AccessibilityObject.ToString(); //workaround to get selecteditems properties refreshed //解决方法以刷新选定项属性

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 : 问题似乎是如果未绘制ListView,则SelectedIndices和SelectedItems无法正确更新,如ListViewItem.Selected属性的MSDN文档中的注释中所述:

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). 如果从未绘制过ListView,则无法信任Selected属性(例如,它位于TabControl中,尚未选中的选项卡中)。 In that case, the SelectedItems and SelectedIndices of the parent ListView are not correctly updated and will still be empty. 在这种情况下,父ListView的SelectedItems和SelectedIndices没有正确更新,仍然是空的。

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. 解决此问题的一个方法是在测试中创建一个简单的虚拟表单类,将ListView添加到表单中,然后只显示表单。 After that the SelectedIndices and SelectedItems properties work as expected. 之后,SelectedIndices和SelectedItems属性按预期工作。

Something like this: 像这样的东西:

    [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对象上调用.CreateControl()会刷新SelectedItems属性。

"I found that calling a .CreateControl() on the ListView object refreshed the SelectedItems properties." “我发现在ListView对象上调用.CreateControl()会刷新SelectedItems属性。”

True, but only if you use Multi-select as well. 是的,但只有你也使用Multi-select。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM