Why can't I capture the wait for FakeItEasy in a variable?

I use FakeItEasy to fake some Entity Framework calls to make sure a bunch of weird outdated image database tables are displayed correctly.

I need to claim that a Client is added to the database with an invoice corresponding to a specific delivery address.

If I do this:

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

the code works fine. I want to optimize the syntax by moving the wait 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();

The test fails. What happens inside the FakeItEasy code, which means that the wait expression works when it is embedded, but cannot be written to a variable and reused later?

+6
source share
3

Ignored In A.CallTo:

Ignored ( _) That A.CallTo. , -. FakeItEasy, , , . FakeItEasy A.CallTo.

, " ". ? FIE 2.0.0, That, ,

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()
+5

, inline, ( , !)

.That.Matches

, . , MustHaveHappened , - . , .

:

Customer addedCustomer;
A.CallTo(() => a.Add(A<Customer>._))
    .Invokes(c => addedCustomer = c.GetArgument<Customer>(0));

//Individual Asserts on addedCustomer
Assert.AreEqual(EXPECTED_ADDRESS, addedCustomer.Invoices.First().Address);
+3

Blair’s answer is correct. I just want to offer a workaround:

// Local function, requires C# 7
Customer ExpectedCustomer() =>
    A<Customer>.That.Matches(
        c => c.Invoices.First().Address == EXPECTED_ADDRESS));

A.CallTo(() => db.Customers.Add(ExpectedCustomer())).MustHaveHappened();
+1
source

Source: https://habr.com/ru/post/1016983/


All Articles