AutoFixture for number ranges

As a one-off you could just do:

var value = fixture.Create<int>() % (max - min + 1) + min;

As a more re-usable approach, you could write an extension method as follows:

public static class FixtureExtensions
{
    public static int CreateInt(this IFixture fixture, int min, int max)
    {
        return fixture.Create<int>() % (max - min + 1) + min;
    }
}

Which can then be used as follows:

var value = fixture.CreateInt(min, max);

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);
}

Tags:

C#

Autofixture