为什么我不能在变量中捕获FakeItEasy期望? [英] Why can't I capture a FakeItEasy expectation in a variable?
问题描述
我正在使用FakeItEasy伪造一些Entity Framework调用,以确保一堆奇怪的遗留数据库表正确映射。
I'm using FakeItEasy to fake some Entity Framework calls, to make sure a bunch of weird legacy database tables are getting mapped properly.
我需要断言具有匹配特定DeliveryAddress的发票的客户正被添加到数据库中。
I need to assert that a Customer with an Invoice matching a specific DeliveryAddress is being added to the database.
如果我这样做:
A.CallTo(() => db.Customers.Add(
A<Customer>.That.Matches(
c => c.Invoices.First().Address == EXPECTED_ADDRESS)
)
)).MustHaveHappened();
代码工作完美。我想通过在其他地方移动期望来简化语法,但是当我这样做时:
the code works perfectly. I want to streamline the syntax by moving the expectation elsewhere, but when i do this:
var expected = A<Customer>.That.Matches(
c => c.Invoices.First().Address == EXPECTED_ADDRESS)
);
A.CallTo(() => db.Customers.Add(expected)).MustHaveHappened();
测试失败。 FakeItEasy代码里面发生什么,意味着期望表达式在内联时工作,但不能被捕获在变量中,并在以后重用?
The test fails. What is happening inside the FakeItEasy code that means the expectation expression works when it's inline but can't be captured in a variable and reused later?
推荐答案
答案在文件中总是放置在A.CallTo中的忽略和
The answer is in the docs at Always place Ignored and That inside A.CallTo:
忽略
(和_
)和那个
匹配器必须放在A.CallTo
调用。这是因为这些特殊约束方法不返回实际的匹配器对象。他们告诉FakeItEasy如何通过被触发的特殊事件匹配参数,然后调用约束方法。 FakeItEasy只侦听A.CallTo
的上下文中的事件。
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开始,使用那个
就像你一样应该抛出一个异常像
I'm surprised the "test fails", though. What version are you using? As of FIE 2.0.0, using That
as you did should throw an exception like
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()
这篇关于为什么我不能在变量中捕获FakeItEasy期望?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!