Fork me on GitHub
Nikos Baxevanis

RangeAttribute support in AutoFixture

Support for types from the System.ComponentModel.DataAnnotations namespace is one of the most voted features for AutoFixture. Starting with version 2.3.1 AutoFixture supports the RangeAttribute class. When this attribute is applied on a data field it can specify the numeric range constraints for it's value.

Let's take as an example the following type:

public class RangeValidatedType
{
    [Range(10, 20)]
    public int Property { get; set; }
} 

Prior to version 2.3.1 if we request an anonymous instance from AutoFixture (or better, a specimen from AutoFixture's kernel) we would get back an instance of the above type with it's Property containing a value probably out of the specified numeric range.

Prior verion 2.3.1

However, after version 2.3.1 AutoFixture can handle requests with numeric range constraints through the RangeAttribute class by issuing a new request for a value inside the specified range.

After verion 2.3.1

An automatically published release created from the latest successful build can be downloaded from here. The latest version is also live on NuGet.