IEnumerable<string> argument

13 views
Skip to first unread message

Peter Neave

unread,
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.
_subClient.DoSomethingAsync("Test").Returns(Task.FromResult(results));
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?

ms.b...@gmail.com

unread,
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

unread,
Aug 17, 2022, 7:18:26 PMAug 17
to nsubs...@googlegroups.com
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 https://groups.google.com/d/topic/nsubstitute/cA04-KQDOC4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nsubstitute...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nsubstitute/972084ba-5f7b-41ff-8f6e-76c517b530d1n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages