<p>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.</p>

<p>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.</p>

<p>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 </p>

<pre><code>item.Selected = true
</code></pre>

<p>and </p>

<pre><code>listView.SelectedIndices.Add(...)
</code></pre>

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

<p>How can I unit test the selection dependent parts of my ListView subclass?</p>

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
:
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.
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.
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);
        }
    }
```
```