Search code examples
c#autofixture

AutoFixture for number ranges


Is there an easy way to do this with AutoFixture?

var myInt = fixture.Create<int>(min, max);

I would like to know whether or not this is possible with AutoFixture or if I have to instantiate a random object and do the work myself.

In case this is not possible, is there a good reason for not having this feature that I am missing here?


Solution

  • Yes, there is:

    // Install-Package AutoFixture.Xunit - or -
    // Install-Package AutoFixture.Xunit2
    
    using System;
    using System.ComponentModel.DataAnnotations;
    using Xunit;
    
    [Theory, AutoData]
    public void ActualIsInTestRange([Range(99, 111)]int actual)
    {
        Assert.InRange(actual, 99, 111);
    }