2
public User AddTags(int userId, List<Tag> Tags)
            {
                var user = GetUserById(userId);
                Tags.ForEach( tag => user.Tags.Add(tag));
                return _repository.Update(user);
            }
public interface IRepository<T> where T:class 
    {
        void Add(T item);
        void Remove(T item);
        T Update(T item);
    }

在测试班

protected IRepository<User> Repository;
protected UserService Service;
public Mock<IRepository<User>> MockUserRepository = new Mock<IRepository<User>>();
Repository = MockUserRepository.Object;
Service = new UserService(UnitOfWork, Repository);
//I don't quite understand how to mock the Update method.
var result = Service.AddUserTags(1,Tags);
Assert.AreEqual(result.UserTags.Count(),3);

我有一个List<User> users和一个List<Tag> tags

我正在使用 EF、NUnit、MOQ 和通用存储库模式。

编辑 :

在 GetUserById 中

return _repository.GetAll().Where(_ => _.UserId == userId).SingleOrDefault();

I have mocked it doing the following 

MockUserRepository.Setup(_ => _.GetAll()).Returns(Users.AsQueryable());
4

2 回答 2

2

我取决于GetUserById(userId);. 为了检查UserTags您必须通过模拟来控制返回User的计数GetUserId计数。

否则,您能做的最好的事情就是确保Update调用该方法:

repository.Verify(x => x.Update(It.IsAny<User>()));

编辑后:然后,您需要设置用户:

[TestFixture]
    public class TestPointer
    {
        private IRepository<User> Repository;
        private UserService Service;
        private Mock<IRepository<User>> MockUserRepository = new Mock<IRepository<User>>();

        [Test]
        public void GetItemsByUserName_UserName_ListOfItems()
        {
            //Arrange
            var unitOfWork = new UnitOfWork();
            Service = new UserService(unitOfWork,  MockUserRepository.Object);
            var Tags = new List<Tag>() { new Tag { Name = "TestTag1" }, new Tag { Name = "TestTag2" }, new Tag { Name = "TestTag3" } };
            var user = new User() {Id = 1};
            MockUserRepository.Setup(x => x.Find(1)).Returns(user);
            MockUserRepository.Setup(x => x.Update(user)).Returns(user);

            //Act
            var result = Service.AddTags(1, Tags);


            //Assert
            Assert.AreEqual(result.Tags.Count(),3);
        }
    }

    public class UserService
    {
        private IRepository<User> _repository;
        private UnitOfWork _unitOfWork;

        public UserService(UnitOfWork unitOfWork, IRepository<User> repository)
        {
            _unitOfWork = unitOfWork;
            _repository = repository;
        }

        public User AddTags(int userId, List<Tag> Tags)
        {
            var user = GetUserById(userId);
            Tags.ForEach(tag => user.Tags.Add(tag));
            return _repository.Update(user);
        }

        private User GetUserById(int userId)
        {
            return _repository.Find(userId);
        }
    }

    public class UnitOfWork
    {

    }

    public interface IRepository<T> where T:class
    {
        T Find(int id);
        void Add(T item);
        void Remove(T item);
        T Update(T item);
    }

    public class User
    {
        private List<Tag> tags = new List<Tag>();

        public int Id { get; set; }
        public List<Tag> Tags
        {
            get { return tags; }
            set { tags = value; }
        }
    }

    public class Tag
    {
        public string Name { get; set; }
    }
于 2012-12-03T09:33:08.390 回答
0

如果您想断言用户发生了什么,则GetuserById应该注入该方法,以便可以将其替换为返回模拟用户的模拟。然后,您可以验证user.Tags.Add(tag)调用的次数是否正确。

之后,您可以验证_repository.Update(user)是否调用了一次;

有关 Moq 中的非常化语法,请查看链接。

于 2012-12-03T10:09:35.567 回答