【问题标题】:Unittesting Confirmation in WPF PrismWPF Prism 中的单元测试确认
【发布时间】:2014-03-27 13:43:09
【问题描述】:

我正在使用 Prism 的 Confirmation 类向用户询问 Confirmation。当我进行单元测试时,确认的返回值总是错误的。也许我可以公开 InteractionRequest 的设置器。我的代码现在看起来像这样,我的单元测试应该验证 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);
    }

【问题讨论】:

    标签: c# wpf unit-testing prism confirmation


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以尝试模拟 copyService 并针对模拟进行测试 Moq的可能实现

      Mock&lt;ICopyService&gt; mockService;

      ....注入VM或具有CopyProjectConfirmationRequest属性的类。

      mockService.Verify(x =&gt; x.Execute(It.Is&lt;Model&gt;(...));

      至于CopyProjectConfirmationRequest,我会将属性公开,并在单元测试中存根。

      【讨论】:

      • 我已经这样做了,但它失败了,因为我的测试中确认结果总是错误的。
      • CopyProjectConfirmationRequest 存根?即把它放在另一个也可以模拟的类中?
      猜你喜欢
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多