如何对以下方法进行单元测试?

时间:2012-12-03 09:00:52

标签: c# entity-framework unit-testing nunit moq

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());

2 个答案:

答案 0 :(得分: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; }
    }

答案 1 :(得分:0)

如果你想断言用户会发生什么,应该注入GetuserById方法,因此可以用mock来替换它,它会返回一个模拟用户。然后,您可以验证user.Tags.Add(tag)被称为正确的次数。

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

对于Moq中的非法语法,请检查this链接。