- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在单元测试中提出一个关键事件。按下某个键时,TextBox 应在其文本属性中包含按下的键。
这是一个使用 Xunit 的最小工作示例:
[TemplatePart(Name = Field0Name, Type = typeof(TextBox))]
class MyControl : Control
{
public const string Field0Name = "Field0";
}
public class MyControlTests
{
private MyControl control;
public MyControlTests()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.Write(@"<ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<Grid>
<TextBox Name='Field0'/>
</Grid>
</ControlTemplate>");
sw.Flush();
ms.Position = 0;
control = new MyControl() { Template = (ControlTemplate)XamlReader.Load(ms) };
control.ApplyTemplate();
}
[Fact]
public void Field0Name_PreviewKeyDownEvent_WriteLetter()
{
TextBox tb = (TextBox)control.Template.FindName(MyControl.Field0Name, control);
FocusManager.SetFocusedElement(control, tb);
tb.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, new FakePresentationSource(), Environment.TickCount, Key.A)
{
RoutedEvent = TextBox.PreviewKeyDownEvent
});
Assert.Equal("a", tb.Text);
}
}
public class FakePresentationSource : PresentationSource
{
protected override CompositionTarget GetCompositionTargetCore()
{
return null;
}
public override Visual RootVisual { get; set; }
public override bool IsDisposed { get { return false; } }
}
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TextBoxRaiseEventProject">
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<TextBox x:Name="Field0"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
测试报告:
最佳答案
对于您的问题,此回复不是您所希望或可能一直期待的。但我认为它是成立的,我希望您在阅读后重新考虑您的单元测试方法。
我认为在单元测试中调用按钮单击并不是对功能进行单元测试的最佳方法。这是 MVVM、MVC、MVP 等模式的原因之一。您想创建一个与 UI 使用分开的可测试类。 (我很抱歉,因为我不想让它听起来像一场讲座)。
因此,在您的示例中,我要做的是创建一个命令,将其绑定(bind)到按钮以供使用,然后通过调用将触发一些可测试组件/部分的命令来进行单元测试。
<Button Command="{Binding PushButtonCommand}"..
some class MyClass
{
public ICommand PushButtonCommand
{
get
{
return _pushButtonCommand ??
(_pushButtonCommand = new DelegateCommand(ExecutePushButton));
}
}
private void ExecutePushButton()
{
//lets pretend it sets some property that you need to test;
NeedBacon = true;
}
现在这是可以测试的了。在创建类后的单元测试中,触发命令 并检查属性:
Assert.IsFalse(myClassInstance.NeedBacon);
((DelegateCommand)myClassInstance.PushButtonCommand).Execute(null);
Assert.IsTrue(myClassInstance.NeedBacon);
关于c# - WPF - 在单元测试中引发 KeyEvent 触发器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13842588/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!