我需要断言,将具有与特定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代码中发生了什么,这意味着期望表达式在内联时有效但无法在变量中捕获并在以后重用?
解决方法
The
Ignored
(and_
) andThat
matchers must be placed within the expression inside theA.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 anA.CallTo
.
不过,我对“测试失败”感到惊讶.你用的是什么版本?截至FIE 2.0.0,正如你所做的那样使用了should throw an exception
system.invalidOperationException : A<T>.Ignored,A<T>._,and A<T>.That can only be used in the context of a call specification with A.CallTo()
原文地址:https://www.jb51.cc/csharp/243026.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。