Search code examples
c#unit-testingautofixture

How to use Verify() for a specific expected result?


Context

I am using AutoFixtue's GuardClauseAssertionto verify the method under test is throwing the expected ArgumentNullException if any of the method call arguments is null:

var fixture = new Fixture();
var assertion = new GuardClauseAssertion(fixture);
var myMethodInfo = ...
assertion.Verify(myMethodInfo);

The method under test looks like:

IEnumerable<T> MyMethod<T>(IList<T> p1, IList<T> p2, Func<T> p3, string p4)
{
     if (p1 == null || p2 == null...)
     {
          yield break;
     }

     // Return non-empty IEnumerable here...
}

Question

In my particular case the expected behavior is not throwing ArgumentNullException, instead return with empty IEnumerable<T>, so I would like to Verify against this result.

How can I customize the existing Verify default behaviour to accomplish this goal?


Solution

  • The GuardClauseAssertion checks if a method parameter is null or not. As you mentioned, your particular case does not fit that behavior, so there is no need in using GuardClauseAssertion. Just create a special test that covers your special behavior.

    In the same time, if you use the assembly- or class-wide guard clause verification, you might need to exclude your particular method from the verification list. AFAIR, AutoFixture does not provide any API for that; you should just use LINQ.