Reputation: 825
I'm trying to figure out a way to create a MockRepository that implements a repository interface. 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:
[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.
Upvotes: 0
Views: 1818
Reputation: 7591
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.
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.
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);
Upvotes: 2