17

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?

Community
  • 1
  • 1
Ties
  • 1,205
  • 2
  • 15
  • 28
  • Do you use plain-AutoFixture or AutoFixture.Xunit/NUnit? – Nikos Baxevanis Sep 25 '15 at 12:16
  • Unfortunately, plain AutoFixture using MSTest. Why? And how would Xunit help, for example? – Ties Sep 25 '15 at 12:18
  • It's less boilerplate with AutoFixture.Xunit. See the [answer](http://stackoverflow.com/a/32782299/467754) below. – Nikos Baxevanis Sep 25 '15 at 12:35
  • 12
    Why not just use the `fixture.CreateMany(2)` ? As i understand from the documentation [link](https://github.com/AutoFixture/AutoFixture/wiki/Version-History#numbers-are-random) the second number will be always > 0 and not another number. – AlexanderW Sep 25 '15 at 12:48
  • 1
    Good catch! Yes, that's easier and works by default, as @MarkSeemann [wrote](http://stackoverflow.com/questions/32781002/how-to-get-autofixture-create-an-integer-that-is-0-and-not-another-number/32782299?noredirect=1#comment53405043_32782299). It should also be the accepted answer. – Nikos Baxevanis Sep 25 '15 at 19:35
  • @AlexanderW I was apparently overlooking something indeed ;) My question had the hidden assumption that AutoFixture didn't do that, but apparently it already does exactly what I want ^_^ – Ties Sep 26 '15 at 14:19
  • @Ties I asked it because i was also not sure. Anyway the problem is solved :) – AlexanderW Sep 29 '15 at 08:04

1 Answers1

20

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
}
Nikos Baxevanis
  • 10,868
  • 2
  • 46
  • 80
  • 11
    This will work, but isn't the requested behaviour the *default* behaviour of AutoFixture? – Mark Seemann Sep 25 '15 at 13:24
  • 1
    OMG, indeed. I was tricked when I first read the original question. [This](http://stackoverflow.com/questions/32781002/how-to-get-autofixture-create-an-integer-that-is-0-and-not-another-number/32782299?noredirect=1#comment53403678_32781002) should be the answer, as commented by @AlexanderW. Good catch! Thank you for pointing that out. – Nikos Baxevanis Sep 25 '15 at 19:33
  • 1
    @NikosBaxevanis You're right, and I've unaccepted your answer. I cannot accept Mark's comment, since it's a comment, of course. Maybe Mark can "promote" his comment, so I can accept it ;) – Ties Sep 26 '15 at 14:22