我需要在给定集合上运行表达式以确定代码中的表达式是否正确编写。出于示例的目的,我将省略一些不必要的上下文,但如果有人需要它,只需发表评论,我将编辑问题并添加您需要的任何内容。
假设我有以下内容:
public interface IRepository
{
IQueryable<T> Query<T>(Expression<Func<T, bool>> expression);
}
public class Repository : IRepository
{
public IQueryable<T> Query<T>(Expression<Func<T, bool>> expression)
{
return _session.Query<T>(expression);
}
}
我想写一个如下的规范:
internal class when_executing_some_query : given_some_repository_context
{
Establish context = () =>
{
IQueryable<SomeObject> objects = new List<SomeObject>
{
SomeObject1,
SomeObject2,
SomeObject3,
}.AsQueryable();
_expectedList = new List<SomeObject>
{
SomeObject1,
SomeObject2,
};
MockedRepository.Setup(x => x.Query<SomeObject>(Moq.It.IsAny<Expression<Func<SomeObject, bool>>>)
.Callback<Expression<Func<SomeObject, bool>>>(expression => _actualExpression = expression);
}
Because of = () => _actualList = objects.Select(_actualExpression).ToList();
It should_execute_on_queryable_and_return_the_expected_items = () => //compare _expectedList with _actualList
}
但是,我Moq.It.IsAny<Expression<Func<SomeObject, bool>>>
说的构建错误
'Project.Domain.IRepository.Query(System.Linq.Expressions.Expression>)' 的最佳重载方法匹配有一些无效参数
和
参数 1:无法从“方法组”转换为
“System.Linq.Expressions.Expression>”`