Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I capture a FakeItEasy expectation in a variable?

I'm using FakeItEasy to fake some Entity Framework calls, to make sure a bunch of weird legacy database tables are getting mapped properly.

I need to assert that a Customer with an Invoice matching a specific DeliveryAddress is being added to the database.

If I do this:

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();

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?

like image 362
Dylan Beattie Avatar asked May 04 '17 11:05

Dylan Beattie


3 Answers

The answer is in the docs at 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.

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()
like image 179
Blair Conrad Avatar answered Nov 14 '22 23:11

Blair Conrad


Not a direct answer for why the expectation expression works inline, but not in a variable (I'm working on that, will edit answer shortly!)

However, I'm not a fan of .That.Matches

The matches can get a bit unwieldy if there's more than one. Plus the MustHaveHappened call will throw an exception if any of the matches fail. Leaving me no idea where the failure happened.

I prefer to do this:

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);
like image 4
Alex Avatar answered Nov 14 '22 23:11

Alex


Blair's answer is correct. I just want to suggest 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();
like image 2
Thomas Levesque Avatar answered Nov 14 '22 23:11

Thomas Levesque