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 8894515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:31:07+00:00 2026-06-14T23:31:07+00:00

I am trying to write a test for the MethodToTest like shown below Public

  • 0

I am trying to write a test for the “MethodToTest” like shown below

Public Class UserService()
{
        private IRepository<User> _userRepo;

        public UserService(IRepository<User> userRepo = null)
        {
            _userRepo = userRepo ?? new UserRepository();
        }

        public List<string> MethodToTest(string userName)
        {
            var user = _userRepo.Find(u => u.Email == userName).First<User>();

            //Other stuff that eventually returns a List<string>
        }
}

I have the following in my test

    [Test]
    public void GetItemsByUserName_UserName_ListOfItems()
    {
        var userName = "AnyString";
        var fakeUserRepo = new Mock<IRepository<User>>();
        var fakeUserList = new List<User>()
        {
        new User()
        {
            Email = userName,
            Roles = new List<Role>()
            {
                new Role()
                {
                    Name="Role1"
                },
                new Role()
                {
                    Name="Role2"
                }
            }
        }
    };
        var fakeUserListQueryable = fakeUserList.AsQueryable<User>();
        var query = new Func<User, bool>(u => u.Email == userName);
        fakeUserRepo.Setup(u => u.Find(query)).Returns(fakeUserListQueryable);
        var userService = new UserService(fakeUserRepo.Object);
        var menu = userService.GetMenuByUserName(userName);

        //Assert Something
    }

The problem is that I can’t get the find method of the _userRepo to return my fake list of users.

When running the test I get a “Sequence Contains No Elements” when executing

_userRepo.Find(u => u.Email == userName).First<User>();

in the MethodToTest.
What am I doing wrong?

  • 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-14T23:31:09+00:00Added an answer on June 14, 2026 at 11:31 pm

    I’m thinking that you have an equality issue on the Func<User, bool>. As you know, Moq will setup the fakeUserRepo if the query equals the actual Func inside the code you are calling. However, you are creating a new Func<User, bool> in your setup code. So, when Moq checks equality, it is checking between two different reference types and therefore not setting up the expectation.

    I would try something like this:

    fakeUserRepo.Setup(u => u.Find(It.IsAny<Func<User, bool>>())).Returns(fakeUserListQueryable);
    

    See if that works and then add the username back in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a test for the following method: public class Sort {
I'm trying to write a test for this class its called Receiver : public
I am trying to write a unit test for the following class: @Transactional public
I am trying to write a test case for the depot application. Below is
I am trying to write a Unit test for a class that has several
Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
I'm trying to write a test for an UrlHelper extensionmethod that is used like
This question arise while trying to write test cases. Foo is a class within
I'm trying to write a test for an ASP.Net MVC controller action. I'd like
I'm trying to write a test for ActiveRecord - and Rails uses MiniTest for

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.