I am trying to use unit test my own WPF control using NUnit. The control is a ListView of buttons bound to a list (DataContext is configured in the control constructor).
I would like to write tests that (for example) add items to the list and verify that a new button is 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 fine when I run my application.
I have included the corresponding code below. What do I need to do to verify 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) {
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);
source share