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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:12:23+00:00 2026-05-26T13:12:23+00:00

Using Moq, I’d like to be able to verify that certain conditions are met

  • 0

Using Moq, I’d like to be able to verify that certain conditions are met on a parameter being passed to a mocked method call. In this scenario, I’d like to check that the list passed into the mocked method is of a certain size:

var mockSomeRepository = new Mock<SomeRepository>();
mockSomeRepository.Setup(m => m.Write(It.IsAny<List<SomeDTO>>())).Verifiable();

var mainClass = new MainClass(mockSomeRepository.Object);
List<SomeDTO> someList = GetListWith25Items();

mainClass.DoRepositoryWrite(someList); // calls SomeRepository.Write(someList);

mockSomeRepository.Verify(m => 
    m.Write(It.Is<List<SomeDTO>>(l => l.Count() == 25)), Times.Once());

The verify assert throws an exception that says the method is never called in this fashion. However, removing the constraint and using Is.Any<List<SomeDTO>>() instead leads to a pass. I’m not sure if I’m using It.Is<>() properly here – this is what I intuitively want my test to look like but I’m not sure if I’m using the framework properly. How should I properly frame this test?

  • 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-05-26T13:12:23+00:00Added an answer on May 26, 2026 at 1:12 pm

    You can get rid of the call to Setup and Verifiable on your mock. Just use Verify.

    I created a little test-project, and this worked for me:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Moq;
    
    namespace csharp
    {
        public class SomeDTO { }
    
        public class SomeRepository { public virtual void Write(List<SomeDTO> list) { } }
    
        public class MainClass
        {
            private SomeRepository someRepository;
    
            public MainClass(SomeRepository someRepository) { this.someRepository = someRepository; }
    
            public void DoRepositoryWrite(List<SomeDTO> list) { this.someRepository.Write(list); }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                var mockSomeRepository = new Mock<SomeRepository>();
                var someList = Enumerable.Repeat(new SomeDTO(), 25).ToList();
    
                var mainClass = new MainClass(mockSomeRepository.Object);
                mainClass.DoRepositoryWrite(someList);
    
                mockSomeRepository.Verify(m => m.Write(It.IsAny<List<SomeDTO>>()), Times.Once(), "Write was not called");
                mockSomeRepository.Verify(m => m.Write(It.Is<List<SomeDTO>>(l => l.Count == 25)), Times.Once(), "Write was not called with a 25-element-list");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my unit-tests I'm mocking a protected method using Moq, and would like to
I'd like to be able to test a class initialises correctly using Moq: class
Using Moq and looked at Callback but I have not been able to find
We are using Moq as our mocking framework, the problem is that type that
I have been using Moq recently in my development process and I like what
I'm using MoQ to test some controllers I have. I'm not able to set
I'm using MOQ to mock a method call with an expected return list. My
I'm using Moq library. I'm mocking up an instance that does all regular CRUD
So, I'm using moq for testing, but I ran into a problem that prevents
I want to mock Find method which expects a predicate using Moq: public PurchaseOrder

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.