Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:05:27+00:00 2026-06-15T11:05:27+00:00

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

  • 0
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());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:05:30+00:00Added an answer on June 15, 2026 at 11:05 am

    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; }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I am calling a function: public User GetUserById(int UserId) { User someUser =
Hello I have like this 2 tables class User public int UserId{get;set;} { ....
class User { public: int v() { return min_pass_len; } static const int min_pass_len
I have 2 entities: public class User { public int userId { get; set;
I have 2 class in Model public class User { public int UserID {
My application has the following entities: public class User { public virtual int UserID
my application has the following entity: public class User { public virtual int UserID
I have a class: public class User { public virtual int Id { get;
my code like below public class User { public int ID { get; set;
Below example of one-to-one relationship throws an exception public class User { public int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.