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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:36:44+00:00 2026-06-12T05:36:44+00:00

I want to verify that a method on my NSubstitute mock is called with

  • 0

I want to verify that a method on my NSubstitute mock is called with a particular array argument.

Say the interface, IProcessor, has a method void ProcessSomething(Foo[] something]). Say my class under test is named Commander. I set up my test like this:

//prepare
var processor = Substitute.For<IProcessor>;
var commander = new Commander(processor);
var foo1 = new Foo("alpha");
var foo2 = new Foo("bravo");
var foos = new [] {foo1, foo2};

//act
commander.DoSomething(foo1, foo2);

//verify
processor.Received().ProcessSomething(foos);  // FAILS

The Received() call fails with:

NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
    ProcessSomething(Foo[])
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
    ProcessSomething(*Foo[]*)

So this looks like ProcessSomething was called with some array other than foos, right?

Well, if I instead test this like, where I capture the argument value using Arg.Do(), it succeeds:

//prepare
//... as before
var actualFoos = null;

processor.ProcessSomething(Arg.Do<Foo[]>(x => actualFoos = x));

//act
commander.DoSomething(foo1, foo2);

//verify
Assert.That(actualFoos, Is.EqualTo(foos));   // SUCCEEDS

So capturing the argument and comparing it for equality (with NUnit in this example) works, but verifying the received call fails.

Is this a bug in NSubstitute, or am I using it 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-12T05:36:46+00:00Added an answer on June 12, 2026 at 5:36 am

    I assume that your Commander object will take the arguments and puts them in an array which it then uses to call the Processor mock.

    Your foos variable is another array which you create on your setup. Arrays don’t compare equal to each other even if they have the same elements. So NSubstitute will complain that it didn’t receive the expected value (it received another array which happened to contain the same elements).

    Edit: Try this version:

    //prepare
    var processor = Substitute.For<IProcessor>;
    var commander = new Commander(processor);
    var foo1 = new Foo("alpha");
    var foo2 = new Foo("bravo");
    var foos = new [] {foo1, foo2};
    
    //act
    commander.DoSomething(foo1, foo2);
    
    //verify
    processor.Received().ProcessSomething(Arg.Is<Foo[]>(foos2 => foos.SequenceEqual(foos2));
    

    This requires importing the System.Linq Namespace

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

Sidebar

Related Questions

I want to test that a method is called on the inner interface, how
I'm using Mockito 1.9.0. How would i verify that a method got called exactly
Here I want to verify that the attribute has changed in my model model
Using RhinoMocks, I have a catch-22 situation: I want to verify that a method
I need to verify that a method was called with an object of a
I need to verify that a method is called, however it receives a parameter
With Mockito, I want to verify() a method call with byte[] in its argument
I want to verify that my understanding is correct. This kind of thing is
I want to verify that the images, css, and javascript files that are part
I want to verify that the mongoose query that I'm creating is using my

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.