我需要一个帮助来解决 .net 中与模拟相关的问题,我有一个带有以下代码的函数。
Task<ICollection<T>> GetAllAsync(Expression<Func<T, bool>> predicate, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, Func<IQueryable<T>, IQueryable<T>> includes = null);
我们有一个 T 作为类,现在我想使用下面的代码进行模拟。
var predict = PredicateBuilder.True<cls1>();
if (DropdownRequestVM?.AllParent == false)
{
predict = predict.And(x => x.Parentid == DropdownRequestVM.ParentID);
}
else
{
predict = predict.And(x => x.Parentid == null);
}
var returneddata = data.AsQueryable().Where(predict).ToList();
this.mockservice
.Setup(repo => repo.Repository
.GetAllAsync(It.IsAny<Expression<Func< cls1, bool>>>(),
It.IsAny<Func<IQueryable< cls1>, IOrderedQueryable< cls1>>>(),
It.IsAny<Func<IQueryable< cls1>, IQueryable< cls1>>>()))
.Returns(Task.FromResult<ICollection< cls1>>(returneddata));
现在我的疑问是。我不想使用 It.isany,因为它允许所有表达式,因此我已经通过了预测,但即使我已经设置了方法。它返回空值。我已经设置了以下方法。
this.mockservice
.Setup(repo => repo.Repository
.GetAllAsync(predict,
It.IsAny<Func<IQueryable< cls1>, IOrderedQueryable< cls1>>>(),
It.IsAny<Func<IQueryable< cls1>, IQueryable< cls1>>>()))
.Returns(Task.FromResult<ICollection< cls1>>(returneddata));
你可以尝试类似的方法,但对语法不是 100% 确定
this.mockservice
.Setup(repo => repo.Repository
.GetAllAsync(It.Is<Expression<Func<cls1,bool>>>(x => LambdaCompare.Eq(x,predict)),It.IsAny<Func<IQueryable<cls1>, IOrderedQueryable<cls1>>>(),It.IsAny<Func<IQueryable<cls1>, IQueryable<cls1>>>()))
.Returns(Task.FromResult<ICollection< cls1>>(returneddata));
我想为下面的代码编写模拟。
GetAllAsync(Expression