NUnitを使用してカスタムWPFコントロールを単体テストしようとしています。コントロールはリストのボタンのListViewです(DataContextはコントロールのコンストラクターに設定されています)。WPFでデータバインディングをユニット単位でテストする
私は、(例えば)リストに項目を追加し、新しいボタンがビューに追加されることを確認するなどのテストを書いたいと思います。しかし、NUnitテストのリストに項目を追加すると、それでもListViewが空であると報告します。私のアプリを実行すると、すべて正常に動作します。
以下の関連コードを記載しました。これをテストするには何をする必要がありますか?
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>
クラス定義
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]
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
}
素晴らしい作品です。ありがとう! –