[英]How to mock a service call with a lambda expression using Moq
I'm using Moq to try to mock a method that has a lambda expression as its optional parameter and calls a database.我正在使用 Moq 来尝试模拟一个将 lambda 表达式作为其可选参数并调用数据库的方法。
This is the real method that I am trying to mock这是我试图模拟的真实方法
public IQueryable<T> AllSearchBy<T>(params Expression<Func<T, bool>>[] search) where T : class
{
IQueryable<T> result = _context.Set<T>();
foreach (var item in search)
{
result = result.Where(item);
}
return result;
}
Here is my moq repository set up这是我的最小起订量存储库设置
var mockRepository = new Mock<IRepository>();
var rateEndHsbSlcTable = new List<Rate_End_HSB_SLC>
{
new Rate_End_HSB_SLC
{
Limit = 10000,
DwellingAgeMin = 0,
DwelilngAgeMax = 25,
Premium = 22M
},
new Rate_End_HSB_SLC
{
Limit = 10000,
DwellingAgeMin = 26,
DwelilngAgeMax = 50,
Premium = 45M
}
};
mockRepository.Setup(m => m.AllSearchBy(It.IsAny<Expression<Func<Rate_End_HSB_SLC, bool>>>()))
.Returns((Expression<Func<Rate_End_HSB_SLC, bool>> predicate) => rateEndHsbSlcTable.Where(predicate.Compile()).AsQueryable());
IRateServices rateService = new RateServices(mockRepository.Object, new HelperServices(mockRepository.Object));
And here is the call in code that I'm testing that gives me an error (I replaced the variables with hard values for simplicity)这是我正在测试的代码中的调用,它给我一个错误(为简单起见,我用硬值替换了变量)
var test = _repository.AllSearchBy<Rate_End_HSB_SLC>(x => x.Limit == 10000 && x.DwellingAgeMin <= 19 && x.DwelilngAgeMax >= 19);
This compiles, but give me the below exception when it runs这编译,但在运行时给我以下异常
System.ArgumentException: Object of type 'System.Linq.Expressions.Expression
1[System.Func
2[Twico.DataAccess.Rate_End_HSB_SLC,System.Boolean]][]' cannot be converted to type 'System.Linq.Expressions.Expression1[System.Func
2[Twico.DataAccess.Rate_End_HSB_SLC,System.Boolean]]'.System.ArgumentException:“System.Linq.Expressions.Expression
1[System.Func
2[Twico.DataAccess.Rate_End_HSB_SLC,System.Boolean]][]”类型的对象无法转换为“System.Linq.Expressions.Expression1[System.Func
”类型1[System.Func
2[Twico.DataAccess.Rate_End_HSB_SLC,System.Boolean]]'。
I suspect the issue is in my setup and return not being in the correct type.我怀疑问题出在我的设置中,返回的类型不正确。
Any ideas on what I'm doing wrong?关于我做错了什么的任何想法?
Edit: using ibebbs answer, I got it to work with this syntax!编辑:使用 ibebbs 答案,我让它可以使用这种语法!
mockRepository.Setup(m => m.AllSearchBy(It.IsAny<Expression<Func<Rate_End_HSB_SLC, bool>>[]>()))
.Returns((Expression<Func<Rate_End_HSB_SLC, bool>>[] predicates) => predicates.Aggregate(rateEndHsbSlcTable, (source, predicate) => source.Where(predicate.Compile()).ToList()).AsQueryable());
Quick look over the code shows that the AllSearchBy
is expecting an array of Expression<Func<T, bool>>
but your setup specifies a single expression.快速查看代码表明
AllSearchBy
需要一个Expression<Func<T, bool>>
数组,但您的设置指定了一个表达式。
Perhaps try something like this?也许尝试这样的事情?
mockRepository
.Setup(m => m.AllSearchBy(It.IsAny<Expression<Func<Rate_End_HSB_SLC, bool>>[]>()))
.Returns(predicates => predicates
.Aggregate(
rateEndHsbSlcTable,
(source, predicate) => source.Where(predicate.Compile())).AsQueryable());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.