WPF Prism中的单元测试确认

3

我正在使用Prism的Confirmation类来询问用户是否确认。当我进行单元测试时,确认返回值总是false。也许我可以将InteractionRequest的setter设置为public。我的代码现在看起来像这样,我的单元测试应该验证是否调用了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);
    }
2个回答

8
解决方案很简单。只需在单元测试中添加以下内容即可。
  sut.CopyProjectConfirmationRequest.Raised += (s, e) =>
  {
    Confirmation context = e.Context as Confirmation;
    context.Confirmed = true;
    e.Callback();
  };

1
您可以尝试模拟copyService并针对模拟进行测试 使用Moq的可能实现 Mock<ICopyService> mockService; ....将其注入VM或具有CopyProjectConfirmationRequest属性的类中。 mockService.Verify(x => x.Execute(It.Is<Model>(...)); 至于CopyProjectConfirmationRequest,我会将其属性设置为public,并在单元测试中进行存根。

我已经尝试过了,但测试中confirmationResult始终为false,导致失败。 - Kingpin
CopyProjectConfirmationRequest 进行桩化 (Stubbing)?即将其放入另一个同样可模拟的类中? - Boklucius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接