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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With