2013-08-29 3 views
3

내가 작성한 동작을 단위 테스트하는 데 문제가 있습니다. 다음과 같이 동작은 다음과 같습니다Unit TextBox 동작 테스트

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) 

이 라우팅 이벤트 인수를 호출해야 이벤트를 인상을 썼다 동일한 테스트하는 동안 turn은 라우트 된 이벤트를 인수로 취합니다.

가짜 RoutedEventArgs를 생성하여 이벤트를 발생시키고 단위 테스트를 수행하는 방법을 알려주세요.

미리 감사드립니다.

답변

1

키보드 입력이 시작되었을 때 명령을 실행하는 단위 테스트 동작이 늦어 질 수 있습니다.

당신은 자세한 내용은 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); 
    } 
    } 
을 찾을 수 있습니다
관련 문제