简体   繁体   English

以下设置不匹配,使用Moq进行WPF MVVM单元测试

[英]The Following Setups were Not Matched, WPF MVVM Unit Testing with Moq

I am writing a unit test in WPF with Moq to test whether or not a user clicked the "Yes" button on a YesNoCancel MessageBox when they are closing the window. 我正在用MoqWPF中编写单元测试,以测试用户关闭窗口时是否单击YesNoCancel MessageBox上的“是”按钮。 In my CloseCommand I have in my ViewModel gives the following message when the user is closing the window: 当用户关闭窗口时,在ViewModel中的CloseCommand我会给出以下消息:

var result = _messageSvc.Show(
    "Do you want to save changes?",
    "Save Changes", 
    Services.MessageBoxButton.YesNoCancel,
    Services.MessageBoxIcon.Question, 
    Services.MessageBoxResult.Yes);

My _messageSvc is a private instance inside of a custom Messaging service to display messageboxes. 我的_messageSvc是自定义消息服务内部的私有实例,用于显示消息框。 Here is the code for the service: 这是该服务的代码:

public interface IMessageSvc
{
    void Show(string message);
    MessageBoxResult Show(string message, string caption, MessageBoxButton buttons, MessageBoxIcon icon, MessageBoxResult defaultResult = 0);
}

public class MessageSvc : IMessageSvc
{
    public void Show(string message)
    {
        MessageBox.Show(message);
    }

    public MessageBoxResult Show(string message, string caption, MessageBoxButton buttons, MessageBoxIcon icon, MessageBoxResult defaultResult)
    {
        return (MessageBoxResult)MessageBox.Show(message, caption, (System.Windows.MessageBoxButton)buttons, (System.Windows.MessageBoxImage)icon, (System.Windows.MessageBoxResult)defaultResult);
    }
}

Note: MessageBoxButton , MessageBoxIcon , and MessageBoxResult are enumerations I made in the same file to mimic what .NET provides out of the box. 注意: MessageBoxButtonMessageBoxIconMessageBoxResult是我在同一文件中进行的枚举,以模仿.NET开箱即用的功能。 I am not showing them here for sake of readability 为了便于阅读,我不在这里显示它们

My unit test is: 我的单元测试是:

[TestMethod]
public void ShouldAskToSaveOnCloseRespondYesTest()
{
    // Mock and setup initial user data 
    var u= new User
    {
        UserId = 1,
        UserName = "FirstName LastName",
        FavoriteColor = "Blue"
    };
    mainViewModel.UserInfo= new UserInfoDtoWrapper(u);

    // assert the data has not changed yet
    Assert.IsFalse(mainViewModel.UserInfo.IsChanged);

    // change the user data
    mainViewModel.UserInfo.UserName= "LastName FirstName";

    // assert the data has changed
    Assert.IsTrue(mainViewModel.UserInfo.IsChanged);

    // execute window closing
    mainViewModel.CloseCommand.Execute(new object());

    // verify messagebox shows and the 'Yes' button is clicked
    // this will then save the changes made to the user instance
    var messageBox = new Mock<IMessageSvc>();
    messageBox.Setup(x => x.Show(It.Is<string>(y => y == "Do you want to save changes?"),
        It.Is<string>(y => y == "Save Changes"),
        It.Is<MessageBoxButton>(y => y == MessageBoxButton.YesNoCancel),
        It.Is<MessageBoxIcon>(y => y == MessageBoxIcon.Question),
        It.Is<MessageBoxResult>(y => y == MessageBoxResult.Yes)))
        .Returns(MessageBoxResult.Yes);

    messageBox.Verify();
}

The error comes on the messageBox.Verify() line, but from what I can see I am doing it correctly. 该错误出现在messageBox.Verify()行上,但是从我看到的信息来看,我做得正确。 I need to get the return value of the messagebox to continue testing what happens after the user clicks the "Yes" button, so I did not want to use Verify with a Linq query inside, instead I need to capture the result. 我需要获取消息框的返回值以继续测试用户单击“是”按钮后发生的情况,因此我不想在内部使用带有Linq查询的“ Verify ,而是需要捕获结果。

Thanks 谢谢

This won't work. 这行不通。 You have 2 issues in your test code: 您的测试代码中有2个问题:

  1. You first fire the CloseCommand (which is assumed to call the IMessageSvc.Show method), and only then setup a mock for that service. 您首先触发CloseCommand (假定它调用了IMessageSvc.Show方法),然后才为该服务设置一个模拟。

  2. You setup a new mock for a service but don't use it. 您为服务设置了新的模拟,但不使用它。

To correct these errors: 要更正这些错误:

  • Change the order such that it is a correct 'arrange' - 'act' - 'assert' order 更改顺序,以使其是正确的“排列”-“动作”-“断言”顺序
  • Inject your mocked service in the view model using dependency injection 使用依赖注入将模拟服务注入视图模型

Here is an example: 这是一个例子:

class MainViewModel
{
    private readonly IMessageSvc _messageSvc;

    public MainViewModel(IMessageSvc svc)
    {
        this._messageSvc = svc;
    }
}

[TestMethod]
public void ShouldAskToSaveOnCloseRespondYesTest()
{
    // Arrange
    var messageBox = new Mock<IMessageSvc>();
    messageBox.Setup(x => x.Show(It.Is<string>(y => y == "Do you want to save changes?"),
        It.Is<string>(y => y == "Save Changes"),
        It.Is<MessageBoxButton>(y => y == MessageBoxButton.YesNoCancel),
        It.Is<MessageBoxIcon>(y => y == MessageBoxIcon.Question),
        It.Is<MessageBoxResult>(y => y == MessageBoxResult.Yes)))
        .Returns(MessageBoxResult.Yes)
        .Verifiable();

    var mainViewModel = new MainViewModel(messageBox.Object);

    // Act
    mainViewModel.CloseCommand.Execute(new object());

    // Assert
    messageBox.Verify();
}

Currently you are setting up your mocked method after testing the code that would call it. 当前,您正在测试将被调用的代码之后,设置模拟方法。 Re-arrange your test into the AAA (Arrange, Act, Assert) pattern like so: 将您的测试重新排列为AAA(排列,执行,声明)模式,如下所示:

var messageBox = new Mock<IMessageSvc>();
messageBox.Setup(x => x.Show(It.Is<string>(y => y == "Do you want to save changes?"),
    It.Is<string>(y => y == "Save Changes"),
    It.Is<MessageBoxButton>(y => y == MessageBoxButton.YesNoCancel),
    It.Is<MessageBoxIcon>(y => y == MessageBoxIcon.Question),
    It.Is<MessageBoxResult>(y => y == MessageBoxResult.Yes)))
    .Returns(MessageBoxResult.Yes);

mainViewModel.CloseCommand.Execute(new object());

messageBox.Verify(x => x.Show(It.Is<string>(y => y == "Do you want to save changes?"),
    It.Is<string>(y => y == "Save Changes"),
    It.Is<MessageBoxButton>(y => y == MessageBoxButton.YesNoCancel),
    It.Is<MessageBoxIcon>(y => y == MessageBoxIcon.Question),
    It.Is<MessageBoxResult>(y => y == MessageBoxResult.Yes)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM