I need one help to solve my mock related question in .net, I have one function with below code.
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.
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. Its returning the 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
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.