1

The commands:Click.Command={Binding OkCommand} in my view (PopupView.xaml)

<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" />

fires off the following command in my viewmodel: (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; }

Basically I want to detect that the OKCommand is indeed called when the button is pressed. So, I need to manually call the OKButton's click event and ultimately detect that ReturnSelectionListsCommand.Execute() and GetAuditOrderByItemList() are each called once to guarantee my popup works correctly. From my research, I see that a ButtonAutomation peer can be used to fire off the click, but in my test, I don't see how I can access the OKButton if I'm using MVVM.

In my unit test, I've attempted to use something along the lines of this:

var mock = new Mock<PopupViewModel>();

to at least detect when methods are called in the viewmodel.

Matthew Steven Monkan
  • 8,170
  • 4
  • 53
  • 71

1 Answers1

1

You might like to consider using project white, which is an abstraction over the MicrosoftUI Automation library.

You could use the API to simulate a click on the button, and then mock your view model to set a flag and assert that it has been set.

devdigital
  • 34,151
  • 9
  • 98
  • 120