8
我有以下的单元测试:为什么我的Moq声明没有引发调用,但它在异常中显示抛出的调用?
[TestMethod]
public void Execute_Sends_Email_To_User()
{
// Setup
InitializeTestEntities();
_mock.Setup(x => x.Send(It.Is<string>(y => y == _user.Email),
It.IsAny<string>(), It.IsAny<string>()));
// Act
new ResetUserPasswordCommand(_unitOfWork,
_mock.Object).WithUserId(_user.Id).Execute();
// Verify
_mock.Verify(x => x.Send("", "", ""), Times.Once());
}
在运行此,我得到下面的异常消息
Test method
MyApp.Tests.Commands.Users.ResetUserPasswordCommandTests.Execute_Sends_Email_To_User
threw exception:
Moq.MockException:
Expected invocation on the mock once, but was 0 times: x => x.Send("", "", "")
Configured setups:
x => x.Send(It.Is<String>(y => y == ._user.Email), It.IsAny<String>(),
It.IsAny<String>()), Times.Once
Performed invocations:
IEmailUtils.Send("[email protected]", "Password Recovery",
"Your new password is: 7Xb79Vb9Dt")
我很困惑这个,因为它说,模拟被invocked 0次,但它表明成功的调用。我究竟做错了什么?
哦,工作!那么'Setup()'方法的重点是什么? – KallDrexx 2011-03-30 04:46:06
使用安装程序/验证方法,您将使用安装程序的唯一原因是返回参数。你仍然可以验证一个方法被调用,如果没有安装提供它不必返回任何东西。这在effdect成为一个存根,但不像Rhino Moq使用安装程序,因为实际上,模拟和存根之间在内部没有区别,它实际上就是您如何使用它的。有些人可能不同意,但它使事情变得简单,更加松散,测试可以集中在一件事上,而不用担心应该发生或不应该发生的模拟。 – aqwert 2011-03-30 19:45:55