I’m trying to unit test a custom WPF control using NUnit. The control is a ListView of buttons, bound to a list (the DataContext is set up in the control’s constructor).
I’d like to write tests which (e.g.) add items to the list and check that a new button gets added to the view, etc. However, when I add an item to the list in my NUnit test, it still reports that the ListView is empty. Everything works OK when I run my app, though.
I’ve included the relevant code below. What do I need to do to test this?
XAML:
<ListView x:Class="SoundBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
ItemsSource="{Binding Path=Sounds}">
<ListView.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Title}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Class definition
public partial class SoundBlock : ListView
{
public SoundBlock(Board xiBoard)
{
// Initialize.
InitializeComponent();
// Set the data context for this block.
DataContext = xiBoard; // Board has an ObservableCollection<Sound>
// property called Sounds.
}
}
Test case
[Test]
public void TestAddSound()
{
board = new Board();
block = new SoundBlock(board);
Assert.AreEqual(0, block.Items.Count);
sound = new Sound("sound.mp3");
board.Sounds.Add(sound);
Assert.AreEqual(1, block.Items.Count); // This fails - count is still 0
}
See my question: Force binding in WPF
You have to force binding, it won’t work until control is shown. You can put it into a new Window and show the window.