c# – 使用AutoFixture 3生成的整数是唯一的吗?
使用IFixture.Create< int>()生成的整数是唯一的吗?
The Wiki says这些数字是随机的,但它也告诉我们这一点
GitHub上的两件相关事情: https://github.com/AutoFixture/AutoFixture/issues/2 https://github.com/AutoFixture/AutoFixture/pull/7 那些单元测试怎么样? https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixtureUnitTest/GeneratorTest.cs#L33 [Theory,ClassData(typeof(CountTestCases))] public void StronglyTypedEnumerationYieldsUniqueValues(int count) { // Fixture setup var sut = new Generator<T>(new Fixture()); // Exercise system var actual = sut.Take(count); // Verify outcome Assert.Equal(count,actual.Distinct().Count()); // Teardown } https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixtureUnitTest/GeneratorTest.cs#L57 [Theory,ClassData(typeof(CountTestCases))] public void WeaklyTypedEnumerationYieldsUniqueValues(int count) { // Fixture setup IEnumerable sut = new Generator<T>(new Fixture()); // Exercise system var actual = sut.OfType<T>().Take(count); // Verify outcome Assert.Equal(count,actual.Distinct().Count()); // Teardown } 我还没有找到一个声明,说明生成的数字是唯一的,只有那些可能暗示它的信息,但我可能错了. 解决方法
目前,AutoFixture努力创造独特的数字,但它并不能保证.例如,您可以耗尽范围,这最有可能发生在字节值上.例如,如果您请求300字节值,您将获得重复项,因为只有256个值可供选择.
一旦初始设置耗尽,AutoFixture将很乐意重用值;另一种方法是抛出异常. 如果对于测试用例而言数字是唯一的很重要,我建议在测试用例本身中明确这一点.您可以组合Generator< T>与众不同: var uniqueIntegers = new Generator<int>(new Fixture()).Distinct().Take(10); 如果您使用的是AutoFixture.Xunit2,则可以请求生成器< T>通过测试方法参数: [Theory,AutoData] public void MyTest(Generator<int> g,string foo) { var uniqueIntegers = g.Distinct().Take(10); // more test code goes here... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |