[英]Moq MockRepository from Interface
I'm trying to figure out a way to create a MockRepository that implements a repository interface. 我正在试图找到一种方法来创建一个实现存储库接口的MockRepository。 Currently, this is what I have: 目前,这就是我所拥有的:
public class MockUserRepository
{
private readonly IList<User> _users;
public MockUserRepository()
{
_users = new List<User>()
{
new User()
{
UserId = new Guid("E480E40F-6802-4296-B2A8-E0D5E018CD41"),
Username = "admin",
Password = "5A105E8B9D40E1329780D62EA2265D8A",
CellPhone = "3175172957"
}
};
}
public IUserRepository Create()
{
var mockUserRepository = new Mock<IUserRepository>();
mockUserRepository.Setup(m => m.Count()).Returns(_users.Count());
mockUserRepository.Setup(m => m.Get()).Returns(_users);
mockUserRepository.Setup(m => m.Get(It.IsAny<Guid>())).Returns((Guid id) => _users.First(x => x.UserId == id));
mockUserRepository.Setup(m => m.Save(It.IsAny<User>())).Returns(
(User user) =>
{
var now = DateTime.Now;
user.ModifiedOn = now;
if(user.UserId.Equals(Guid.Empty))
{
user.CreatedOn = now;
_users.Add(user);
}
else
{
var original = _users.First(x => x.UserId == user.UserId);
if (original == null)
{
return false;
}
original = user;
}
return true;
}
);
mockUserRepository.Setup(m => m.Find(It.IsAny<IList<object[]>>())).Returns(
(IList<object[]> criteria) =>
{
IList<User> uList = _users.ToList();
foreach (object[] criteriaItem in criteria)
{
var name = (string)criteriaItem[0];
var value = (string)criteriaItem[1];
IList<User> tempList = uList.ToList();
IList<User> addList = tempList.Where(user => (string) user.GetType().GetProperty(name).GetValue(user, null) == value).ToList();
uList = addList;
}
return uList;
});
return mockUserRepository.Object;
}
}
I would like to have something like this: 我想要这样的东西:
public class MockUserRepository : IUserRepository
Then, just have all of my mocked methods actually implemented. 然后,让我所有的模拟方法实际实现。 I want to do this to enforce my developers to fully mock out a repository based on the interface. 我想这样做是为了强制我的开发人员基于接口完全模拟出一个存储库。
From the test side, this Mocked Repository is used like this: 从测试方面来看,这个Mocked Repository的使用方式如下:
[SetUp]
public void Setup()
{
MockUserRepository = new MockUserRepository().Create();
MockRoleRepository = new MockRoleRepository().Create();
MockQuestionRepository = new MockQuestionRepository().Create();
AuthenticationService = new AuthenticationService(MockUserRepository, MockRoleRepository, MockQuestionRepository);
}
All of this works just fine, but I'm really stuck on enforcing my MockRepository to implement all of its interface's methods. 所有这一切都很好,但我真的坚持强制执行我的MockRepository来实现其所有接口的方法。
this is over-complicating the use of mocks. 这使得模拟的使用过于复杂。 simply Mock the IUserRepository interface in the test and then mock the methods you need based on the tests. 只需在测试中模拟IUserRepository接口,然后根据测试模拟所需的方法。
reposistory = new Mock<IUserRepository>();
var id = 1;
var user = new User();
repository.Setup(x=> x.Get(id)).Returns(user);
var sut = new Service(repository.Object);
var result = sut.Get(id);
Assert.Equals(user, result);
If you really want a "blank" object just implement a Fake IUserRepository and forgo a mocking framework altogether. 如果你真的想要一个“空白”对象,只需实现一个Fake IUserRepository并完全放弃一个模拟框架。
class FakeUserRepository : IUserRepository
{
public List<User> Users = new List<Users>();
public User Get(long id)
{
return Users.FristOrDetail(x=>x.Id = id);
}
}
var id = 1;
var user = new User();
var repository = new FakeUserRepository();
repository.Users.Add(user);
var sut = new Service(repository);
var result = sut.Get(id);
Assert.Equals(user, result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.