Question

In an AutoFixture-based test, I'm trying to express as cleanly as possible the following:

When I pass <input> to the parameter x of this method, filling in the other parameters anonymously, the result is...

Taking the example of a factory method:-

class X
{
    public static X Create( Guid a, Guid b, Guid c, String x, String y);

I'm trying to express as a succinct series of tests:

  1. If I pass null for x, it should throw
  2. If I pass null for y, it should throw

In order to express I can say:

var fixture = Fixture();
var sut = default( Func<Guid, Guid, Guid,string,X>);
sut = fixture.Get( ( Guid anonA, Guid anonB, Guid anonC, string anonY ) => 
    x =>
        X.Create( anonA, anonB, anonC, x, anonY ) );

Assert.Throws<ArgumentNullException>( () => sut( null));

For the second instance, which is only slightly different, I need to do:

var fixture = Fixture();
var sut = default( Func<Guid, Guid, Guid,string,X> );
sut = fixture.Get( ( Guid anonA, Guid anonB, Guid anonC, string anonX ) => 
    y =>
        X.Create( anonA, anonB, anonC, anonX, y ) );
Assert.Throws<ArgumentNullException>( () => sut( null));

For properties, there's With in AutoFixture. Is there an eqivalent for method (and/or ctor) arguments ?

PS 0. I don't mind if its necessary to get into 'magic' strings for this case - i.e., having the x bit be "x".

PS 1. The other elephant in the room is that I'm bumping my head against the 4x overloads of Get in AutoFixture - or is that because I have an old version in this environment?

PS 2. Also open to better suggestions as to how to model this - as long as they deal with the fact that I want it to be a method invocation and not properties or fields (and I'd like it to work in an AutoFixture stylee).

Was it helpful?

Solution

There really isn't any feature in AutoFixture that makes this easier, but I'm open to suggestions. However, I don't see how you could express something like that in a strongly typed fashion. What would the syntax look like?

However, if you only need this for testing that Null Guards work, you can use AutoFixture.Idioms for that.

Here's an example.

var fixture = new Fixture();
var assertion = new GuardClauseAssertion(fixture);
var method = typeof(GuardedMethodHost).GetMethod("ConsumeStringAndInt32AndGuid");
assertion.Verify(method);

If you look at the source code of Ploeh.AutoFixture.IdiomsUnitTest.Scenario you'll find other examples, but I admit it's one of the more poorly documented areas of AutoFixture...

Another thing entirely is that methods with few (or no) parameters are better than methods with many parameters, so have you considered Introducing a Parameter Object?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top