如何在谓词调用中测试与FakeItEasy的匹配?

 黎明前后2502887455 发布于 2023-01-30 15:44

我的代码中有以下调用:

var dbResults = new List();
dbResults = dateTimeFilter == null
    ? new List(
        CrossReferenceRelationshipRepository.GetAll()
                .ToList().OrderBy(crr => crr.ToPartner))
    : new List(
        CrossReferenceRelationshipRepository.SearchFor(
            crr => crr.HistoricEntries
                .Any(he => he.ModifiedDatetime > dateTimeFilter))
                .ToList().OrderBy(crr => crr.ToPartner));

我正在尝试使用FakeItEasy来验证当dateTimeFilter有一个值时,SearchFor(…)正在我的存储库中使用正确的函数调用它.

所以我的测试看起来像这样:

A.CallTo(() => crossReferenceRelationshipRepositoryMock.SearchFor(A>>.That
    .Matches(exp => Expression.Lambda>(((BinaryExpression)exp.Body).Right).Compile().Invoke() == filterByDate)))
    .MustHaveHappened(Repeated.Exactly.Once);

哪个不对.什么是测试我是否SearchFor(…)使用正确表达式调用的方法?

crr => crr.HistoricEntries.Any(he => he.ModifiedDatetime > dateTimeFilter)

传入的实际值SearchFor(…)DateTime.MinValue因此我将断言更改为:

A.CallTo(() => crossReferenceRelationshipRepositoryMock.SearchFor(A>>.That
    .Matches(exp => Expression.Lambda>(((BinaryExpression)exp.Body).Right).Compile().Invoke() == DateTime.MinValue)))
    .MustHaveHappened(Repeated.Exactly.Once);

这是失败的,我得到的例外是

System.InvalidCastException:
  Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN'
  to type 'System.Linq.Expressions.BinaryExpression'.

而且我不确定我做错了什么......

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有