Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

697
Visualizações
Can Autofixture.Create<int> return a negative value?

Actually, the following method always returns a positive integer:

var fixture = new Fixture();
var someInt = fixture.Create<int>();

Is it possible that one day, the feature evolves and begins to return a negative integer? Or is there an official reason it actually returns an always positive integer
If not, is there some documentation hidden somewhere telling it?

To keep on using autofixture, I'd like to know if some changes are foreseen.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The author of Autofixture discusses this on his blog. This post specifies that the current implementation will always return positive numbers since they are deemed "safer" in general, so I don't think this will change in the near future.

The whole point of AutoFixture is to generate anonymous test data. You are asking for an integer which can be a negative number. To be 100% safe, I wouldn't rely on the implicit assumption that all future implementations only return positive numbers. You can make this more explicit by providing a custom SpecimenBuilder:

fixture.Customizations.Add(new PositiveIntegerBuilder());

More information about custom specimen builders can be found here.

over 4 years ago · Santiago Trujillo Relatório

0

As user prgmtc points out, one option is through a custom ISpecimenBuilder.

Another option is to supply a custom range, by using the built-in RandomNumericSequenceGenerator class, as shown below:

[Fact]
public void FixtureCreatesNegativeNumbers()
{
    var fixture = new Fixture();
    fixture.Customizations.Add(
        new RandomNumericSequenceGenerator(-900, -100));

    var i = fixture.Create<int>();
    // Prints -> -711
    var l = fixture.Create<long>();
    // Prints -> 618
    var f = fixture.Create<float>();
    // Prints -> -78.0
}
over 4 years ago · Santiago Trujillo Relatório

0

var i = fixture.Create<uint>();

Only if the original code being tested expected a uint type parameter.

Wouldvar i = Math.Abs(fixture.Create<int>()) work for you

To answer the original question

  • int by defintion can be negative.
  • fixture.Create, by definition should not return negative int.

I would assume that the authors of AutoFixture will not change something that crucial so lightly. I would use fixture.Create<int>

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda