tho*_*att 6 c# data-binding wpf nunit
我正在尝试使用NUnit对自定义WPF控件进行单元测试.该控件是按钮的ListView,绑定到列表(DataContext在控件的构造函数中设置).
我想编写测试(例如)将项目添加到列表中并检查是否有新按钮添加到视图中等等.但是,当我在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>
Run Code Online (Sandbox Code Playgroud)
类定义
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.
}
}
Run Code Online (Sandbox Code Playgroud)
测试用例
[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
}
Run Code Online (Sandbox Code Playgroud)