Unit Testing TextBox Behavior

I am having problems with the behavior testing module that I wrote. The behavior is as follows:

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

During module testing, I wrote this code

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

Now, to raise an event, I need to call

 textBoxInvoker.RaiseEvent(routedEventArgs) 

in this argument, arged routing, in turn, takes a routed event as an argument.

Please help me create a mock RoutedEventArgs to raise this event and then Unit test behavior.

Thanks in advance.

+4
source share
1 answer

It may be late, but here is the unit test behavior that executes the command when calling Keyboard Enter.

More here and here

  [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); } } 
+2
source

Source: https://habr.com/ru/post/1499612/


All Articles