user5884770
user5884770

Reputation:

How do you mock IEnumerable<T> ElementAt() using NSubstitute?

I have been having issues implementing the correct technique to mock out the Linq methods for an IEnumerable.

{
    var qs = Substitute.For<IEnumerable<object>>();
    qs.ElementAt(i).Returns(q);
    qs.Count().Returns(i);
}

Which is meant to test a method, which contains both pulling an item out of the list at random or in a controlled fashion.

private IEnumerable<T> list;
async object Save(object obj, byte i = 0)
{
     var random = new Random();
     var index = i == 0 ? random.Next(0, list.Count()) : i;
     return list.ElementAt(index);
}

Doing this results in:

at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)
   at King.Azure.Unit.Test.Data.StorageQueueShardsTests.<Save>d__11.MoveNext() in C:\Users\jefkin\Documents\GitHub\King.Azure\King.Azure.Unit.Test\Data\StorageQueueShardsTests.cs:line 127
--- End of stack trace from previous location where exception was thrown ---
   at NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(Object invocationResult)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunAsyncTestMethod(TestExecutionContext context)
Result Message: 
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Upvotes: 1

Views: 1717

Answers (1)

Marcio Rinaldi
Marcio Rinaldi

Reputation: 3513

The only methods that can be mocked are either interface methods or virtual methods from classes.

Since ElementAt is an extension method and not a IEnumerable<T> method, it can't be mocked.

The best way to test this would be to use a List<T> instead and setup the list accordingly and not using NSubstitute.

For example:

var qs = new List<object> {
    obj1,
    obj2,
    obj3
};

var result = sut.Save(obj, i);

var expected = qs[i];
Assert.Equal(expected, result);

Upvotes: 3

Related Questions