NSubstitute 模拟可枚举 Where() 方法

NSubstitue mock IEnumerable Where() method

我有以下代码:

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

但我收到以下错误:

System.ArgumentNullException : 值不能为空。 参数名称:谓词 在 System.Linq.Enumerable.Where[TSource](IEnumerable'1 source, Func'2 predicate) 在第 46 行。

但是,不采用 func<> 个参数的方法(例如 Any())不会失败。

我的问题是:

NSubstitute 不能像 Enumerable.Where<T> 那样模拟扩展方法。对于 List 的情况,我建议根本不要嘲笑它。使用真实的列表并添加测试所需的项目,以便真实的 Where(..) 扩展方法将筛选项目并提供所需的项目。