moodmosaic

StringLengthAttribute support in AutoFixture

Continuing the support of DataAnnotations as described here, there is now support for the StringLengthAttribute class. Starting with version 2.4.0, when this attribute is applied to a data field, it can specify the maximum length of characters that are allowed.

Let’s take as an example the following type:

public class StringLengthValidatedType
{
    [StringLength(3)]
    public string Property { get; set; }
}

Prior to version 2.4.0, if we requested an anonymous instance from AutoFixture, by default we would get back an instance of the above type with its Property containing a value similar to the one below.

However, after version 2.4.0, AutoFixture can handle requests with string length constraints through the StringLengthAttribute class by issuing a new request for a constrained string.

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