IEnumerable<string> argument

Skip to first unread message

Peter Neave

Aug 17, 2022, 7:47:04 AMAug 17
to NSubstitute
I've been having issues with setting up Args.Any<IEnumerable<string>>() in my substitute. Check out this example (I wrote it by hand - so the syntax might not be exactly correct).

I want to substitute this method `DoSomethingAsync`
_subClient.DoSomethingAsync(string query, IEnumerable<string> returnFields);

This doesn't work - for some reason it's not recognising an string array as an IEnumerable<string>
_subClient.DoSomethingAsync("Test", Any.Args<string[]>()).Returns(Task.FromResult(results));
var response = await _subClient.DoSomethingAsync("Test", new string[]{"id","name"});

If I remove it then it matches up and returns the value that I'm expecting.
var response = await _subClient.DoSomethingAsync("Test");

I've tried using a List<string> and returning .AsEnumerable() but that didn't work ether.

What am I doing wrong?

Aug 17, 2022, 10:21:34 AMAug 17
to NSubstitute
Have you tried this way?

_subClient.DoSomethingAsync("Test", Any.Args<IEnumerable<string>>()).Returns(Task.FromResult(results)); 

Peter Neave

Aug 17, 2022, 7:18:26 PMAug 17
Yes, I have. Going to a string array was out of desperation.

I just tried this code
_subBoxClient.SearchManager.QueryAsync(query: "Test", fields: Arg.Any<IEnumerable<string>>()).Returns(Task.FromResult(results));
IEnumerable<string> fields = new List<string>{ "id" };
var u = await _subBoxClient.SearchManager.QueryAsync(query: "Test", fields: fields.AsEnumerable<string>());

The variable 'u' is still null. If I remove the 'fields' (which has a default of null btw) parameter then it returns what I expect.

You received this message because you are subscribed to a topic in the Google Groups "NSubstitute" group.
To unsubscribe from this topic, visit
To unsubscribe from this group and all its topics, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages