0

so the code under test is:

var query = _documentClient.CreateDocumentQuery<TEntity>(CollectionUri, GetFeedOptions()).AsQueryable();

foreach (var filter in filters)
{
    query = query.Where(filter);      
}

It throws an exception at query = query.Where(filter); where my filter is a valid Expression.

The code works at runtime, and compiles, but my unit tests are throwing the exception:

Value cannot be null.
Parameter name: arg0

at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0, Expression arg1)


at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)

at .DataAccess.Core.CosmoDbRepositoryBase`1.GetAsync(IEnumerable`1 filters, Int32 take, Boolean getAll) in C:\...DataAccess\Core\CosmoDbRepositoryBase.cs:line 223

However, my arg0 is not null Debug View My Mock, using XUnit & NSubstitute, looks like this:

IDocumentClient _documentClient = Substitute.For<IDocumentClient>();
var document = new Document();
document.LoadFrom(new JsonTextReader(new StringReader(JsonConvert.SerializeObject(TestDataFactory.GetFakeResourceEntity()))));
var response = new ResourceResponse<Document>(document);
_documentClient.CreateDocumentAsync(Arg.Any<Uri>(), Arg.Any<object>(), Arg.Any<RequestOptions>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
            .Returns(Task.FromResult(response));
Robert Green MBA
  • 1,834
  • 1
  • 22
  • 45
  • 1
    static methods - ands that´s what extension-methods are - are hard to mock. Having said this you shouldn´t probily mock the where-statement, but replace the underlying collection by an in-memory one. – MakePeaceGreatAgain Jun 07 '19 at 15:30

1 Answers1

1

You don't mock .Where(predicate). You mock predicate itself. Basically, collection.Where(_ => true) will always be just collection with no modification at all!

Zazaeil
  • 3,900
  • 2
  • 14
  • 31