How to get AutoFixture create an integer that is >

2019-04-25 19:03发布

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条回答
We Are One
2楼-- · 2019-04-25 19:30

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
}
查看更多
登录 后发表回答