我是最小起订量新手,我已阅读快速入门here https://github.com/Moq/moq4/wiki/Quickstart。我使用的是最小起订量 v4.2.1402.2112。我正在尝试创建一个单元测试来更新人员对象。这UpdatePerson
方法返回更新后的 person 对象。有人可以告诉我如何纠正这个问题吗?
我收到此错误:
Moq.MockException was unhandled by user code
HResult=-2146233088
Message=Error updating Person object
Expected invocation on the mock once, but was 0 times: svc => svc.UpdatePerson(.expected)
Configured setups: svc => svc.UpdatePerson(It.IsAny<Person>()), Times.Never
No invocations performed.
Source=Moq
IsVerificationError=true
这是我的代码:
[TestMethod]
public void UpdatePersonTest()
{
var expected = new Person()
{
PersonId = new Guid("some guid value"),
FirstName = "dev",
LastName = "test update",
UserName = "[email protected] /cdn-cgi/l/email-protection",
Password = "password",
Salt = "6519",
Status = (int)StatusTypes.Active
};
PersonMock.Setup(svc => svc.UpdatePerson(It.IsAny<Person>()))
.Returns(expected)
.Verifiable();
var actual = PersonProxy.UpdatePerson(expected);
PersonMock.Verify(svc => svc.UpdatePerson(It.IsAny<Person>()), Times.Once(), "Error updating Person object");
Assert.AreEqual(expected, actual, "Not the same.");
}
有了这条线
PersonMock.Verify(svc => svc.UpdatePerson(It.IsAny<Person>()),
Times.Once(), // here
"Error updating Person object");
您正在对模拟设定期望UpdatePerson
方法应该被调用一次。它失败了,因为您的 SUT(您正在测试的类)根本不调用此方法:
没有执行任何调用
还要验证您是否将模拟对象传递给PersonProxy
。它应该是这样的:
PersonProxy = new PersonProxy(PersonMock.Object);
及实施
public class PersonProxy
{
private IPersonService service; // assume you are mocking this interface
public PersonProxy(IPersonService service) // constructor injection
{
this.service = service;
}
public Person UpdatePerson(Person person)
{
return service.UpdatePerson(person);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)