如何为Dialog服务MVVM Light编写测试用例 [英] How to write test case for Dialog services MVVM Light

查看:185
本文介绍了如何为Dialog服务MVVM Light编写测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVVM的新手,我正在关注MVVM Light工具包. 为了实现消息框功能,我进行了搜索并获取了例子

I am new very new to MVVM and i am following MVVM Light toolkit. To implement a message box functionality i searched and got this example

我不明白的要点是

  1. DialogService 类是我们应该通过继承 IDialogService ??
  2. 创建的类
  1. Class DialogService is what we should create by inheriting IDialogService ??

如果是这种情况,例如在继承接口后的类中,我们需要编写如下内容

If that is the case for example in the class after inheriting the interface we need to write something like below

 public Task<bool> ShowMessage(string message, string title, string buttonConfirmText, string buttonCancelText, Action<bool> afterHideCallback)
 {
            //Here a kind of this implemetation i need to do ?
            //For example sake i didn't include the parameters.
  MessageBox.Show();
 }

如果以上观点在视图模型中成立,我们将 dialogServices.ShowMessage(prms ....)吗? 那么如何进行测试呢?

If the above is true in the view model we will just dialogueServices.ShowMessage(prms....) ? So how this can be tested ?

例如,如果它是filebrowser,则如果我们根据理解在单元测试中调用这种方式,它将打开文件浏览器. 如何实现呢?

For instance if it is filebrowser , if we invoke this way in unit test as per understanding it will open a file browser. How this can be implemented?

由于我非常喜欢这种模式,所以我很难理解.

Since i am very to this pattern itself i am finding difficulty in understanding.

请提供示例实现或任何参考.

Please provide a sample implementation or any reference.

编辑

我也引用了此链接.在评论中,有人告知它有观点的责任. 我比较困惑.一般来说,至少对于确认对话框,我们将如何做?

I referred this link also. In comment it was told its responsibility of a view. I was more confused. In general at least for confirmation dialog's how would we do it ?

谢谢.

推荐答案

请勿在您的ViewModel/Business逻辑中混合UI逻辑. 您应该单独考虑业务逻辑,UI逻辑.

Don't mix UI logic in your ViewModel/Business logic. You should seperate concerns like business logic, UI logic.

我建议您使用MvvmLight Messenger工具.您的ViewModel应该发布消息有一个显示信息的对话框",并且适当的视图应该注册,收听和处理该消息.

I suggest you to use MvvmLight Messenger facilities. Your ViewModel should publish message "there is an informational dialog to show" and appropiate view should register, listen and handle this message.

示例:

ViewModel

ViewModel

public void SomeViewModelMethod() {
     if (somethingWentWrong)
        Messenger.Default.Publish(new ShowInformationalDialogMessage(title,msg));
}

查看

.. OnLoaded { 
   Messenger.Default.Register<ShowInformationalDialogMessage>( () => {
      UI LOGIC CODE
   });
}

要测试这种情况,您只需在单元测试类中注册已发布的消息并断言此消息逻辑已被执行.

To test this case you should just register to published message in your unit test class and assert that this message logic has been executed.

这篇关于如何为Dialog服务MVVM Light编写测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆