简体   繁体   English

用没有 It.isany 的谓词模拟

[英]mock with predicate without It.isany

I need one help to solve my mock related question in .net, I have one function with below code.我需要一个帮助来解决我在 .net 中的模拟相关问题,我有一个 function 代码如下。

Task<ICollection<T>> GetAllAsync(Expression<Func<T, bool>> predicate, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, Func<IQueryable<T>, IQueryable<T>> includes = null);

Where we have a T as class, now I want to use the below code for the mocking.我们有一个 T 为 class,现在我想将以下代码用于 mocking。

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));

Now my query is.现在我的查询是。 I do not want to use the It.isany, as its allow all the expression, so that I have passed predict, but even I have setup the methods.我不想使用 It.isany,因为它允许所有表达式,所以我已经通过了预测,但即使我已经设置了方法。 Its returning the null.它返回 null。 I have setup the method below way.我已经设置了以下方法。

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));

You could try something like, not 100% sure about the syntax您可以尝试类似的方法,但不能 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM