我的代码中有以下调用:
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'.
而且我不确定我做错了什么......