How to get AutoFixture create an integer that is >0, and not another number?

Ties picture Ties · Sep 25, 2015 · Viewed 8.4k times · Source

I want AutoFixture to generate two integers, and for the second one, I don't want it to be 0, or the previous generated number. Is there a way to tell AutoFixture to honor that "requirement".

Looking at RandomNumericSequenceGenerator, I looks like the lower limit is 1, so I might not have to specify the first requirement. Next, I was looking at the "seeding" option, but as indicated in this answer, it won't be used for a number, by default.

Is there something I'm overlooking here?

Answer

Nikos Baxevanis picture Nikos Baxevanis · Sep 25, 2015

Here's a way to do this with plain AutoFixture:

[Fact]
public void GenerateTwoDistinctNonZeroIntegersWithAutoFixture()
{
    var fixture = new Fixture();
    var generator = fixture.Create<Generator<int>>();

    var numbers = generator.Where(x => x != 0).Distinct().Take(2);
    // -> 72, 117
}

And here's a way to do this with AutoFixture.Xunit:

[Theory, AutoData]
public void GenerateTwoDistinctNonZeroIntegersWithAutoFixtureXunit(
    Generator<int> generator)
{
    var numbers = generator.Where(x => x != 0).Distinct().Take(2);
    // -> 72, 117
}