c# – 为什么我不能在变量中捕获FakeItEasy期望值?

c# – 为什么我不能在变量中捕获FakeItEasy期望值?,第1张

概述我正在使用FakeItEasy伪造一些实体框架调用,以确保正确映射一堆奇怪的遗留数据库表. 我需要断言,将具有与特定DeliveryAddress匹配的发票的客户添加到数据库中. 如果我这样做: A.CallTo(() => db.Customers.Add( A<Customer>.That.Matches( c => c.Invoices.First().Address 我正在使用FakeItEasy伪造一些实体框架调用,以确保正确映射一堆奇怪的遗留数据库表.

我需要断言,将具有与特定DeliveryAddress匹配的发票的客户添加到数据库中.

如果我这样做:

A.CallTo(() => db.Customers.Add(    A<Customer>.That.Matches(        c => c.Invoices.First().Address == EXPECTED_ADDRESS)    ))).MustHaveHappened();

代码完美无缺.我希望通过在其他地方移动期望来简化语法,但是当我这样做时:

var expected = A<Customer>.That.Matches(    c => c.Invoices.First().Address == EXPECTED_ADDRESS));A.CallTo(() => db.Customers.Add(expected)).MustHaveHappened();

测试失败. FakeItEasy代码中发生了什么,这意味着期望表达式在内联时有效但无法在变量捕获并在以后重用?

解决方法 答案是在 Always place Ignored and That inside A.CallTo的文档中:

The Ignored (and _) and That matchers must be placed within the Expression insIDe the A.CallTo call. This is because these special constraint methods do not return an actual matcher object. They tell FakeItEasy how to match the parameter via a special event that’s fired then the constraint method is invoked. FakeItEasy only Listens to the events in the context of an A.CallTo.

不过,我对“测试失败”感到惊讶.你用的是什么版本?截至FIE 2.0.0,正如你所做的那样使用了should throw an exception

system.invalIDOperationException : A<T>.Ignored,A<T>._,and A<T>.Thatcan only be used in the context of a call specification with A.CallTo()
总结

以上是内存溢出为你收集整理的c# – 为什么我不能在变量中捕获FakeItEasy期望值?全部内容,希望文章能够帮你解决c# – 为什么我不能在变量中捕获FakeItEasy期望值?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1248139.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存