Unit testing MOQ错误预期在模拟上调用一次,但为0次
我刚接触MOQ,我读过《快速入门》。我使用的是MOQ v4.2.1402.2112。我正在尝试创建一个单元测试来更新person对象。Unit testing MOQ错误预期在模拟上调用一次,但为0次,unit-testing,moq,Unit Testing,Moq,我刚接触MOQ,我读过《快速入门》。我使用的是MOQ v4.2.1402.2112。我正在尝试创建一个单元测试来更新person对象。UpdatePerson方法返回更新的person对象。有人能告诉我怎么改正吗 我得到这个错误: Moq.MockException was unhandled by user code HResult=-2146233088 Message=Error updating Person object Expected invocation on the mock
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
Moq.MockException未由用户代码处理
HResult=-2146233088
消息=更新Person对象时出错
预期对模拟调用一次,但为0次:svc=>svc.UpdatePerson(.Expected)
配置的设置:svc=>svc.UpdatePerson(It.IsAny()),Times.Never
没有执行任何调用。
来源=最小起订量
IsVerificationError=true
这是我的密码:
[TestMethod]
public void UpdatePersonTest()
{
var expected = new Person()
{
PersonId = new Guid("some guid value"),
FirstName = "dev",
LastName = "test update",
UserName = "dev@test.com",
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.");
}
[TestMethod]
public void UpdatePersonTest()
{
var预期值=新人()
{
PersonId=新Guid(“某些Guid值”),
FirstName=“dev”,
LastName=“测试更新”,
用户名=”dev@test.com",
Password=“Password”,
Salt=“6519”,
状态=(int)StatusTypes.Active
};
Setup(svc=>svc.UpdatePerson(It.IsAny())
.回报(预期)
.可验证();
var-actual=PersonProxy.UpdatePerson(预期);
Verify(svc=>svc.UpdatePerson(It.IsAny())、Times.Once()、“更新Person对象时出错”);
AreEqual(预期的,实际的,“不一样”);
}
使用此行
PersonMock.Verify(svc => svc.UpdatePerson(It.IsAny<Person>()),
Times.Once(), // here
"Error updating Person 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);
}
}
向我们展示您正在测试的方法。感谢您的详细解释
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);
}
}