开发者

How can I unit test a WPF StyleSelector?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜