2012-01-12 19 views
1

的命令:Click.Command={Binding OkCommand}在我的视图(PopupView.xaml使用Moq,如何检测按钮点击是否触发了我的viewmodel中的事件?

<Button 
    Name="OKButton" 
    Content="{Binding Path=foo.foo, Source={StaticResource LocalResourceWrapper}}" 
    ToolTipService.ToolTip="{Binding Path=foo.foo, Source={StaticResource LocalResourceWrapper}}" 
    HorizontalAlignment="Center" 
    commands:Click.Command="{Binding OkCommand}" 
    Margin="5,10,5,10" 
    Click="OK_Click" 
    TabIndex="1" /> 

打完在我的视图模型下面的命令:(PopupViewModel.cs)

private ICommand _okCommand; 

    public ICommand OkCommand 
    { 
     get 
     { 
      if (_okCommand == null) 
      { 
       _okCommand = new DelegateCommand<object>(OnOKCommand); 
      } 
      return _okCommand; 
     } 
    } 

    public void OnOKCommand(object someObject) 
    { 
     this.ReturnSelectionListsCommand.Execute(this.GetAuditOrderByItemList()); 
    } 

    public DelegateCommand<List<AuditOrderByItem>> ReturnSelectionListsCommand { get; set; } 

基本上我想检测出OKCommand的确当按下按钮时调用。所以,我需要手动调用OKButton的点击事件,并最终检测到ReturnSelectionListsCommand.Execute()GetAuditOrderByItemList()每次都被调用一次以保证我的弹出窗口正常工作。从我的研究中,我发现可以使用ButtonAutomation对等点来触发点击,但在我的测试中,如果使用MVVM,我看不到如何访问OKButton。

在我的单元测试,我试图按照这种线使用的东西:

var mock = new Mock<PopupViewModel>(); 

至少检测时,方法调用的视图模型。

回答

1

您可能会考虑使用project white,这是对MicrosoftUI自动化库的抽象。

您可以使用API​​来模拟按钮上的单击,然后模拟您的视图模型以设置标志并声明它已被设置。

相关问题