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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:28:04+00:00 2026-06-13T10:28:04+00:00

I have a class that takes an IRepository in its constructor like this …

  • 0

I have a class that takes an IRepository in its constructor like this …

    public class UserService
    {
       public IRepository<User> _repo { get; set; }
       public UserService(IRepository<User> repo = null)
       {
        _repo = repo ?? new UserRepository();
       }

and has a method that looks like this …

public bool IsUserActive(email string)
{
   //The method actually does more but to keep it simple lets imagine 
   // it does this
   User user = _repo.Find(u => u.Email == email).First();
   return user.IsActive;
}

The IRepository looks like this.

public interface IRepository<T> : IDisposable where T : IEntity
{
    void InsertOrUpdate(T entity);
    void Delete(T entity);
    IQueryable<T> Find(Func<T, bool> query);
    T Find(int id);
    void Save();
}

I would like to test the IsUserActive method and make sure it returns false if the IsActive field of the user is false, and vice versa.

I’m trying the following …

    [Test]
    public void IsUserActive_EmailThatWillReturnInactiveUser_ReturnsFalse()
    {
        //Arrange
        var fakeUserRepo = new Mock<IRepository<User>>();
        var query = new Func<User, bool>(u => u.Email == "AnyString");
        var listOfMatchingUsers = new List<User>() 
        {
                    new User 
                    { 
                        Email = "AnyString", 
                        IsActive = False 
                    }
        };
        IQueryable<User> queryableUsers = listOfMatchingUsers.AsQueryable();
        fakeUserRepo.Setup(r => r.Find(query)).Returns(queryableUsers);
        var userService = new UserService(fakeUserRepo.Object);

        //Act
        var result = userService.IsUserActive("AnyString");

        //Assert
        Assert.IsFalse(result);
    }

When I run in NUnit I get the Error “Sequence Contains No Elements” on this line

var result = userService.IsUserActive("AnyString");

Where am I going 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-13T10:28:06+00:00Added an answer on June 13, 2026 at 10:28 am

    Change mock setup to

    fakeUserRepo.Setup(r => r.Find(It.IsAny<Func<User, bool>>()))
                .Returns(queryableUsers);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic class that takes a type T . Within this class
So I have a base class with a constructor that takes (T V) .
I have a wcf service that takes in an IRepository IRepository irepo; public SomeService(IRepository
I have a utility class that takes a constructor parameter of an interface to
Let's say I have a service interface that looks like this: public interface IFooService
I am creating unit tests with DUnit. I have a class that takes quite
I have a class with two constructors, one that takes no arguments and one
I have a Login Class which has a function: isCorrect() that takes username and
I have a method that takes a list of entities ( Class es) and
I have a static method that takes a parameter and returns a class. the

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.