Oleg Kosuakiv
Oleg Kosuakiv

Reputation: 173

NSubstitue mock IEnumerable Where() method

I have the following code:

45 var listMock = Substitute.For<List<EntityTestObject>>(); 46 listMock.Where(Arg.Any<Func<EntityTestObject, bool>>()).Returns(Something);

But I'm getting the following error:

System.ArgumentNullException : Value cannot be null. Parameter name: predicate at System.Linq.Enumerable.Where[TSource](IEnumerable'1 source, Func'2 predicate) at line 46.

However, methods that does not take func<> arguments, such as Any(), do not fail.

My questions are:

Upvotes: 1

Views: 933

Answers (1)

David Tchepak
David Tchepak

Reputation: 10484

NSubstitute can not mock extension methods like Enumerable.Where<T>. For the case of a List I would advise not mocking it at all. Use a real list and add the items you need for your test, so that the real Where(..) extension method will filter the items and provide the items required.

Upvotes: 3

Related Questions