More often than not, I want to assert that my dependency has been called with a parameter that matches more than one condition.

Let’s say we have a method that calls ICustomerService.Add method.
We want to assert the Add method was called with a Customer that matches a few conditions.

The way we’d typically achieve this is by doing something like this:

This will work. And it will indeed assert that the AddCustomer method was called with a Customer object, with those properties set to the corresponding values.

The problem lies if one of those properties doesn’t match.
We get a failed test. But no idication of which property it’s failed on.

What I like to do instead, is follow this pattern:

So, here, we’re basically saying when Add is called, with any Customer object, Invoke the anonymous function, that takes the Customer argument, and stores it in addedCustomer.

We can then individually assert on each property.

Note
I created an issue on GitHub which addresses this, with a view to finding a more ‘inline’ solution

While running my tests, I noticed that if I ran my test suite all together, some would fail, giving the exception:

API restriction: The assembly has already loaded from a different location

This issue was happening both in ReSharper test runner, and using the NUnit test runner

Debugging, the exception was being thrown on my SetUp methods like:

I posted about this on StackOverflow, and tweeted contributors of the FakeItEasy project.

At the time of writing, there is an immediate workaround:

Run tests in parallel.

To do this for the ReSharper test runner, do the following:

Options -> Unit Testing -> Run up to x assemblies in parallel.
(Set to anything greater than 1)

How to run tests in parallel with resharper

 

An issue has been created on GitHub for this:

https://github.com/FakeItEasy/FakeItEasy/issues/189