NSubstitute: Checking received methods with array arguments

后端 未结 1 1408
广开言路
广开言路 2021-01-03 17:31

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

相关标签:
1条回答
  • 2021-01-03 18:19

    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 讨论(0)
提交回复
热议问题