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

  • Home
  • SEARCH
  • 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 5968483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:05:47+00:00 2026-05-22T20:05:47+00:00

I have a repository with an Add method that takes an IEnumerable as parameter:

  • 0

I have a repository with an Add method that takes an IEnumerable as parameter:

public void Add<T>(T item) where T : class, new(){}

In a unittest I want to verify that this method is called with an IEnumerable that contains exactly the same amount of elements as another IEnumerable

[Test]
public void InvoicesAreGeneratedForAllStudents()
{
    var students = StudentStub.GetStudents();
    session.Setup(x => x.All<Student>()).Returns(students.AsQueryable());

    service.GenerateInvoices(Payments.Jaar, DateTime.Now); 

    session.Verify(x => x.Add(It.Is<IEnumerable<Invoice>>(
        invoices => invoices.Count() == students.Count())));
 }

Result of the unit test:

Moq.MockException : 
Expected invocation on the mock at least once, but was never performed: 

x => x.Add<Invoice>(It.Is<IEnumerable`1>(i => i.Count<Invoice>() == 10))

No setups configured.

What am I doing wrong?

  • 1 1 Answer
  • 2 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-05-22T20:05:48+00:00Added an answer on May 22, 2026 at 8:05 pm

    From your code example you haven’t set up the x => x.Add on the Moq

    session.Setup(x => x.Add(It.IsAny<IEnumerable>());
    

    Unless the Setup for x.All is meant to be x.Add? If so, you need to match the Verify and Setup exactly – a good way to do that is to extract it to a common method that returns an Expression.

    EDIT: Added a sample, I have changed the signature of Add as I can’t see how you could pass a collection otherwise.

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Mock<Boo> moqBoo = new Mock<Boo>();
            moqBoo.Setup(IEnumerableHasExpectedNumberOfElements(10));
    
            // ACT
    
            moqBoo.Verify(IEnumerableHasExpectedNumberOfElements(10));
        }
    
        private static Expression<Action<Boo>> IEnumerableHasExpectedNumberOfElements(int expectedNumberOfElements)
        {
            return b => b.Add(It.Is<IEnumerable<Invoice>>(ie => ie.Count() == expectedNumberOfElements));
        }
    }
    
    public class Boo
    {
        public void Add<T>(IEnumerable<T> item) where T : class, new()
        {
        }
    }
    
    public class Invoice
    {
    
    }
    

    Also, a good way to debug these things is to set your Mock up with MockBehavior.Strict and then you’ll be informed by the invoked code what you need to configure.

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

Sidebar

Related Questions

I have a Repository Class with the following method... public T Single<T>(Predicate<T> expression) {
I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
I have the following repository that I use for unit testing: public class MyTestRepository<T>
I have a repository Class which takes in a ObjectContext called TestDB. I when
I have the following action method that initiated SelectList as follow:- public PartialViewResult Search(string
I have a repository class that uses an NHibernate session to persist objects to
In my service facade layer, I have a service class with a method/operation that
Say I have two separate classes, A and B. I also have Repository class
I have a repository on my PC. Now I want to move some project
I currently have a repository setup that loads forms a person would need to

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.