[英]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. 我正在用Moq在WPF中编写单元测试,以测试用户关闭窗口时是否单击
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. 注意:
MessageBoxButton
, MessageBoxIcon
和MessageBoxResult
是我在同一文件中进行的枚举,以模仿.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个问题:
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
方法),然后才为该服务设置一个模拟。
You setup a new mock for a service but don't use it. 您为服务设置了新的模拟,但不使用它。
To correct these errors: 要更正这些错误:
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.