WPF棱镜中的单元测试确认

Kin*_*pin 3 c# wpf unit-testing prism confirmation

我正在使用Prism的Confirmation类来询问用户确认.当我进行单元测试时,确认的返回值始终为false.也许我可以公开InteractionRequest的setter.我的代码现在看起来像这样,我的单元测试应该验证调用this.copyService.Execute.

public InteractionRequest<Confirmation> CopyProjectConfirmationRequest { get; private set; }

    bool confirmationResult = false;
    DialogConfirmation dialogConfirmation = new DialogConfirmation
                                              {
                                                Title = "Copy and Convert Project",
                                                Content = string.Format("This Project was created with Version {0} to be used with currentVersion({1}) it must be converted should it copyed and converted Project", projectVersion, toolVersion),
                                                ConfirmButtonText = "Copy & Convert",
                                                DeclineButtonText = "Cancel"
                                              };

    this.CopyProjectConfirmationRequest .Raise(dialogConfirmation, cb => { confirmationResult = cb.Confirmed; });

    if (confirmationResult)
    {
      this.copyService.Execute(this.Model);
    }
Run Code Online (Sandbox Code Playgroud)

Kin*_*pin 8

解决方案很简单.只需在UnitTest中添加它

  sut.CopyProjectConfirmationRequest.Raised += (s, e) =>
  {
    Confirmation context = e.Context as Confirmation;
    context.Confirmed = true;
    e.Callback();
  };
Run Code Online (Sandbox Code Playgroud)