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.
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
and
listView.SelectedIndices.Add(...)
But SelectedIndices or SelectedItems simply does not appear to be affected. 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?
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:
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: