IEnumerable<string> argument

43 views
Skip to first unread message

Peter Neave

unread,
Aug 17, 2022, 7:47:04 AM8/17/22
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 AM8/17/22
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 PM8/17/22
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.

David Tchepak

unread,
Jan 17, 2023, 11:07:50 PM1/17/23
to nsubs...@googlegroups.com
Could you please post a reproducible example?

This example works for me:

```
    public interface Client
    {
        Task<int> DoSomethingAsync(string query, IEnumerable<String> returnFields);
    }

    public class TestFixture
    {
        [Fact]
        public async Task Test1Async() {
            var subClient = Substitute.For<Client>();
            subClient.DoSomethingAsync("Test", Arg.Any<IEnumerable<string>>()).Returns(42);

            var result = await subClient.DoSomethingAsync("Test", new[] { "a", "b" });

            Assert.Equal(42, result);
        }
    }
```

You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nsubstitute/CACp%2BpOgU%3Dhco68yeTzyOfVT6-j0KCKrz%3D0d_89tHtHD3e76pfA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages