-2

I am trying to declare and initialize an empty IEnumerable in a Moq statement. For some reason the last part where I try to declare ProductResult as Enumerable is failing. Can i do this inline with Moq statement?

mock.Setup(b => b.GetProductStatus(It.IsAny<bool>())).Returns(new List<ProductResult>().AsEnumerable() { });

Trying to research this article:

IEnumerable<object> a = new IEnumerable<object>(); Can I do this?

3 Answers3

0

I can only make a wild guess with this limited info. Can you please remove .AsEnumerable() { } and see?

  1. The {} is odd and it's not proper C# code.
  2. List implements IEnumerable so you don't need the .AsEnumerable()
sidecus
  • 732
  • 5
  • 10
0

You moq should look like this. No need to do AsEnumerable as List is an implementation of IEnumerable.

 mock.Setup(x => x.GetProductStatus(It.IsAny<bool>()))
.Returns(new List<ProductResult>());

You method I beleive looks like this:

IEnumerable<ProductResult> GetProductStatus(bool someValue);

With a list you can do things like Count, Linq Any etc.

For further info, Quickstart is a good reference. Link here: https://code.google.com/archive/p/moq/wikis/QuickStart.wiki

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
0

If you just need to return empty IEnumerable you can use Enumerable.Empty. If you need non empty collection, then you can return any, because all of them implement IEnumerable.

.Returns(Enumerable.Empty<ProductResult>())
.Returns(new[] {1, 2, 3})
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
fenixil
  • 2,106
  • 7
  • 13