我有一个简单的窗口,其中引用了 App.xaml 中的 StaticResource。
App.xaml资源定义:
<!-- Standard Text Box Style -->
<Style x:Key="textBoxStyleStd" TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="14" />
</Style>
使用资源的窗口组件:
<TextBlock Grid.Column="1" Grid.Row="0" Name="stationIdTitle"
Style="{StaticResource textBlockStyleStd}"
VerticalAlignment="Center" HorizontalAlignment="Center"
Text="{LocText Key=Title, Dict={StaticResource Dictionary},
Assembly={StaticResource Assembly}}"/>
当尝试对该窗口进行单元测试时,我收到错误:
System.Windows.Markup.XamlParseException:找不到名为的资源
'{textBlockStyleStd}'。资源名称区分大小写。错误于
标记文件中的对象“stationIdTitle”
'Zpg;component/guicomponenets/screens/enterstationidscreen.xaml' 行
23 位置 71。
有没有办法解决?我的单元测试代码是:
[Test]
public void TestEnterKeyPressedNoText()
{
IPickingBusinessObject pickingBusinessObject = mock.StrictMock<IPickingBusinessObject>();
EnterStationIdScreen objectUnderTest = new EnterStationIdScreen(pickingBusinessObject);
Assert.AreEqual(Visibility.Visible, objectUnderTest.stationIdError.Visibility);
Assert.AreEqual("werwe", "oksdf");
Replay();
objectUnderTest.EnterKeyPressed();
Verify();
}
谢谢肯特,
我查看了您的建议,在大多数情况下我同意应该使用和测试模型,但是,有一些与控件相关的代码(例如 TextBox 可见性)我仍然想测试。为了解决这个问题,您可以创建应用程序的实例(但不初始化它)并手动添加资源。这确实会导致 App.xaml 和基本单元测试重复,但这使我能够完成我想要的测试。
if (Application.Current == null)
{
App application = new App();
#region Add Static Resources from the App.xaml
Style textBoxStyle = new Style(typeof(TextBox));
textBoxStyle.Setters.Add(new Setter(TextBox.FontSizeProperty, 14d));
Style textBlockStyle = new Style(typeof(TextBlock));
textBlockStyle.Setters.Add(new Setter(TextBlock.FontSizeProperty, 14d));
application.Resources.Add("TextBoxStyleStd", textBoxStyle);
application.Resources.Add("TextBlockStyleStd", textBlockStyle);
application.Resources.Add("TextBlockStyleError", textBlockStyle);
application.Resources.Add("Assembly", "Zpg");
#endregion
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)