Heuristics for Static Factory Methods in AutoFixture

Aug 25, 2011

Here is a type with a private constructor:

public class TypeWithFactoryMethod
{
    private TypeWithFactoryMethod() 
    {
    }

    public static TypeWithFactoryMethod Create()
    {
        return new TypeWithFactoryMethod();
    }

    public static TypeWithFactoryMethod Create(object argument)
    {
        return new TypeWithFactoryMethod();
    }
}

In order to create an instance of that type we have to call one of it's static factory methods, for example:

var instance = TypeWithFactoryMethod.Create();

If we try to create an Anonymous Variable with AutoFixture right now (version 2.1) it will throw an exception since there are no public constructors:

var fixture = new Fixture();
var result = fixture.CreateAnonymous<TypeWithFactoryMethod>();

Using the latest version from trunk (and on the next public release) the above code will work. It will successfully return an instance of the type by using a set of heuristics that enable AutoFixture to search for static factory methods.

The latest build (including strong names) can be downloaded from here.

Combining Data Theories in AutoFixture.Xunit extension

Aug 25, 2011

xUnit.net supports parameterized tests via data theories which are types deriving from the Xunit.Extensions.DataAttribute type. Some popular attributes include:

InlineDataAttribute, PropertyDataAttribute and ClassDataAttribute.

AutoFixture.Xunit extension includes a very useful type, for providing auto-data theories, called AutoMoqDataAttribute

Imagine a Scenario where we have a unit test method with 3 parameters. We want the first parameters to be supplied by InlineData and the rest by AutoFixture using AutoMoqData.

[Theory]
[InlineAutoData("foo")]
[InlineAutoData("foo", "bar")]
public void Test(string s1, string s2, MyClass myClass)
{
}

This is now possible using the InlineAutoDataAttribute type which is now on the trunk and will be available on next public release (after version 2.1). It provides a data source for a data theory, with the data coming from inline values combined with auto-generated data specimens generated by AutoFixture.

InlineAutoDataAttribute derives from CompositeDataAttribute, an implementation of DataAttribute that composes other DataAttribute instances.

Here is how it works:

  • Delegate the GetData method call to the first attribute.
  • Loop through the results from the first attribute and check if the length of each object array matches the number of arguments required by the method.
    • If so, yield the object array.
    • If not, invoke GetData on the next attribute.
      • Throw away the first objects from the result in order to continue from where it ran out of objects from the first result set and concatenate the rest to the previous array.
  • Recursively repeat this process until all object arrays have the required length.

While this feature comes with AutoFixture.Xunit extension, the CompositeDataAttribute class depends only on the xunit.extensions assembly.

You may compile the code from the latest trunk version, alternatively the latest build (including strong names) can be downloaded from here.

subscribe via RSS