ashutosh raina
ashutosh raina

Reputation: 9314

How to Unit test the following method?

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

In the test class

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

I have a List<User> users and a List<Tag> tags.

I am using EF , NUnit ,MOQ and the generic repository pattern.

Edit :

In GetUserById

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

I have mocked it doing the following 

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

Upvotes: 2

Views: 396

Answers (2)

Morten
Morten

Reputation: 3844

If you want to assert on what happens with the user, The GetuserById method should should be injected, so it can be replaced by a mock, that returns a mocked user. You can then verify, that the user.Tags.Add(tag) is called the right number of times.

After that, you can verify that _repository.Update(user) is called one time;

For veryfication syntax in Moq, check this link.

Upvotes: 0

Davin Tryon
Davin Tryon

Reputation: 67296

I depends on what happens in GetUserById(userId);. In order to check the count of UserTags you must control the returned User by mocking the GetUserId.

Otherwise, the best you can do is to make sure that the Update method is called:

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

After edit: Then, you would need to setup the 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; }
    }

Upvotes: 2

Related Questions