C# 使用 Moq 模拟返回空值的更新方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15805812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mock an update method returning a void with Moq
提问by Kris-I
In my test, I defined as data a List<IUser>
with some record in.
在我的测试中,我将数据定义为List<IUser>
带有一些记录的数据。
I'd like setup a moq the methode Update
, this method receive the user id
and the string
to update.
我想设置一个 moq 方法Update
,这个方法接收用户id
和string
更新。
Then I get the the IUser
and update the property LastName
然后我得到IUser
并更新属性LastName
I tried this :
我试过这个:
namespace Tests.UnitTests
{
[TestClass]
public class UsersTest
{
public IUsers MockUsersRepo;
readonly Mock<IUsers> _mockUserRepo = new Mock<IUsers>();
private List<IUser> _users = new List<IUser>();
[TestInitialize()]
public void MyTestInitialize()
{
_users = new List<IUser>
{
new User { Id = 1, Firsname = "A", Lastname = "AA", IsValid = true },
new User { Id = 1, Firsname = "B", Lastname = "BB", IsValid = true }
};
Mock<IAction> mockUserRepository = new Mock<IAction>();
_mockUserRepo.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
.Returns(???);
MockUsersRepo = _mockUserRepo.Object;
}
[TestMethod]
public void Update()
{
//Use the mock here
}
}
}
But I get this error : cannot resolve Returns symbole
但我收到此错误:无法解析返回符号
Do you have an id ?
你有身吗?
class User : IUser
{
public int Id { get; set; }
public string Firsname { get; set; }
public string Lastname { get; set; }
public bool IsValid { get; set; }
}
interface IUser
{
int Id { get; set; }
string Firsname { get; set; }
string Lastname { get; set; }
bool IsValid { get; set; }
}
interface IAction
{
List<IUser> GetList(bool isActive);
void Update(int id, string lastname)
}
class Action : IAction
{
public IUser GetById(int id)
{
//....
}
public void Update(int id, string lastname)
{
var userToUpdate = GetById(id);
userToUpdate.LastName = lastname;
//....
}
}
回答by Ufuk Hac?o?ullar?
If you just want to verify this method is called, you should use Verifiable() method.
如果你只是想验证这个方法是否被调用,你应该使用 Verifiable() 方法。
_mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
.Verifiable();
If you also want to do something with those parameters, use Callback() first.
如果您还想对这些参数执行某些操作,请先使用 Callback()。
_mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
.Callback((int id, string lastName) => {
//do something
}).Verifiable();
Update
更新
Here's how you should mock it if you return a bool value as result.
如果你返回一个 bool 值作为结果,你应该如何模拟它。
_mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
.Returns(true);
回答by Vyacheslav Volkov
Mock<IUsers> _mockUserRepository = new Mock<IUsers>();
_mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))
.Callback((int id, string name) =>
{
//Your callback method here
});
//check to see how many times the method was called
_mockUserRepository.Verify(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()), Times.Once());