views:

54

answers:

1

I'm having trouble figuring out a way to unit test a WPF style selector.

My selector looks like:

public class ListViewItemStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var listView = ItemsControl.ItemsControlFromItemContainer(container) as ListView;

        Style style;

        var index = listView.ItemContainerGenerator.IndexFromContainer(container);

        if (index % 2 == 0)
            style = (Style)listView.FindResource("listViewItemStyle");
        else
            style = (Style)listView.FindResource("listViewAlternatingItemStyle");

        return style;
    }
}

I'd have to think there would be a way to mimic the binding process and then assert on the style that comes out. Any ideas, or is this an area of WPF that can't be faked out?

I'm using Rhino Mocks for my mocking framework, but I'm not opposed to hand rolling fakes if need be.

+1  A: 

You're clearly in the realm of View (as opposed to Model or ViewModel) testing.

The simplest way to verify this behavior is with GUI unit testing with a tool like IcuTest (http://IcuTest.com). You simply create a list with a few items, display it, and verify that it's alternating.

Ray
This could very well end up being the accepted answer, but I'm holding out hope for awhile yet that there is a code only testing solution. If I can't test this process via code only, then it means Microsoft can't either. That would be unfortunate, but I guess not too difficult to believe.
Daniel Auger