来自Interface的Moq MockRepository

时间:2012-09-20 19:07:34

标签: c# interface moq

我正在试图找到一种方法来创建一个实现存储库接口的MockRepository。目前,这就是我所拥有的:

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;
        }
    }

我想有这样的事情:

public class MockUserRepository : IUserRepository

然后,让我所有的模拟方法实际实现。我想这样做是为了强制我的开发人员基于接口完全模拟出一个存储库。

从测试方面来看,这个Mocked Repository的使用方式如下:

[SetUp]
    public void Setup()
    {
        MockUserRepository = new MockUserRepository().Create();
        MockRoleRepository = new MockRoleRepository().Create();
        MockQuestionRepository = new MockQuestionRepository().Create();

        AuthenticationService = new AuthenticationService(MockUserRepository, MockRoleRepository, MockQuestionRepository);
    }

所有这一切都很好,但我真的不得不强制执行我的MockRepository来实现其所有接口的方法。

1 个答案:

答案 0 :(得分:2)

这使得模拟的使用过于复杂。只需在测试中模拟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);

如果你真的想要一个“空白”对象,只需实现一个伪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);