gpt4 book ai didi

c# - 单元测试文本框行为

转载 作者:行者123 更新时间:2023-11-30 12:29:26 25 4
gpt4 key购买 nike

我在对我编写的行为进行单元测试时遇到问题。行为如下:

NumericTextBoxBehavior : Behavior<TextBox>
{
//handles few events like TextChanged ,PreviewTextInput , PreviewKeyDown , PreviewLostKeyboardFocus
//to give make it accept numeric values only

}

虽然单元测试相同,但我编写了这段代码

TextBox textBoxInvoker = new TextBox();
NumericTextBoxBehavior target = new NumericTextBoxBehavior();
System.Windows.Interactivity.Interaction.GetBehaviors(TextBoxInvoker).Add(target);

现在要引发我必须调用的事件

textBoxInvoker.RaiseEvent(routedEventArgs)

此路由事件参数依次将路由事件作为参数。

请帮助我如何创建模拟 RoutedEventArgs 以引发事件并进一步对行为进行单元测试。

提前致谢。

最佳答案

可能会迟到,但这是一种单元测试行为的方法,它在调用键盘输入时执行命令。

您可以找到更多信息herehere

  [TestFixture]
public class ExecuteCommandOnEnterBehaviorFixture
{
private ExecuteCommandOnEnterBehavior _keyboardEnterBehavior;
private TextBox _textBox;
private bool _enterWasCalled = false;


[SetUp]
public void Setup()
{
_textBox = new TextBox();
_keyboardEnterBehavior = new ExecuteCommandOnEnterBehavior();
_keyboardEnterBehavior.ExecuteCommand = new Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand<object>((o) => { _enterWasCalled = true; });
_keyboardEnterBehavior.Attach(_textBox);
}

[Test]
[STAThread]
public void AssociatedObjectClick_Test_with_ItemClick()
{
_textBox.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
MockRepository.GenerateMock<PresentationSource>(),
0,
Key.Enter) { RoutedEvent = Keyboard.KeyDownEvent });

Assert.That(_enterWasCalled);
}
}

关于c# - 单元测试文本框行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18510781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com