4

I have code which looks like:

eventPublisher.Publish(new SpecificEvent(stuff),
                        EventStreams.Stream1,
                        EventStreams.Stream2);

which is calling a method defined as :

Publish<T>(T eventToPublish, params EventStream[] streams) where T : IEvent;

in something I want to test. This event publication is the most important thing to happen in what I want to test, but I am not interested in testing which event streams it publishes to. How can I make a substitute in NSubstitute to test that this is called with an appropriate event, without concerning myself with the params? Thus far, I have:

eventPublisher.Received(1).Publish(Arg.Any<SpecificEvent>());

This, of course, does not match the call with two streams. Is there a way to match a params argument using NSubstitute, ignoring the number of parameters passed in?

penguat
  • 1,337
  • 1
  • 13
  • 25

1 Answers1

10

Params enables methods to receive variable numbers of parameters. With params, the arguments passed to a method are changed by the compiler to elements in a temporary array. This array is then used in the receiving method.

You can use Arg.Any<EventStream[]> to match a params argument, ignoring number of parameters passed in.

eventPublisher.Received(1).Publish(Arg.Any<SpecificEvent>(), Arg.Any<EventStream[]>())
Valentin
  • 5,380
  • 2
  • 24
  • 38
  • I'm slightly embarassed that when I tried this, I must've got something else wrong! Thankyou for the quick and correct answer. – penguat Feb 25 '16 at 11:55
  • I like to leave it for a couple of hours, in case some stupendous answer comes along and stuns me - now accepted. – penguat Feb 25 '16 at 16:26