Home - Tags - Bookmarks - About

RangeAttribute support in AutoFixture

Tuesday, 13 September 2011

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.

━━━

This post has been filed under #autofixture

Feedback

Have feedback on this page? Let @nikosbaxevanis know on twitter. Need help or found a bug? Contact me.


© 2011-2018 Nikos Baxevanis. All written content on this site is provided under a Creative Commons ShareAlike license. All code is provided under a MIT license unless otherwise stated.