我希望 AutoFixture 生成两个整数,对于第二个整数,我不希望它为 0 或之前生成的数字。有没有办法告诉 AutoFixture 遵守该“要求”。
看看RandomNumericSequenceGenerator
,我看起来像 lower limit is 1 ,所以我可能不必指定第一个要求。接下来,我正在查看“播种”选项,但如this answer中所示,默认情况下它不会用于数字。
这里有我忽略的东西吗?
最佳答案
这里有一种使用普通 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
}
这是一种使用 AutoFixture.Xunit 来做到这一点的方法。 :
[Theory, AutoData]
public void GenerateTwoDistinctNonZeroIntegersWithAutoFixtureXunit(
Generator<int> generator)
{
var numbers = generator.Where(x => x != 0).Distinct().Take(2);
// -> 72, 117
}
关于c# - 如何让 AutoFixture 创建一个 >0 的整数,而不是另一个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32781002/