在我的测试中,我将 List<IUser>
定义为数据,在。
我想设置一个 moq 的方法 Update
,这个方法接收用户 id
和 string
更新。
然后我得到 IUser
并更新属性 LastName
我试过了:
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
}
}
}
但是我得到了这个错误: 无法解析“返回”符号
查到身份了吗?
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;
//....
}
}