所以要测试的代码是:
var query = _documentClient.CreateDocumentQuery<TEntity>(CollectionUri, GetFeedOptions()).AsQueryable();
foreach (var filter in filters)
{
query = query.Where(filter);
}
它在query = query.Where(filter);
处引发异常,其中我的过滤器是有效的表达式。
代码可以在运行时运行并编译,但是我的单元测试抛出了异常:
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
但是,我的arg0
不为null
使用XUnit
和NSubstitute
的“我的嘲笑”看起来像这样:
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));
答案 0 :(得分:1)
您不会嘲笑.Where(predicate)
。您嘲笑predicate
本身。基本上,collection.Where(_ => true)
永远只是collection
,根本没有任何修改!