How to get AutoFixture create an integer that is >

2019-04-25 19:24发布

问题:

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?

回答1:

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
}