如何让AutoFixture创建一个大于0的整数,而不是另一个数字? [英] How to get AutoFixture create an integer that is >0, and not another number?

查看:125
本文介绍了如何让AutoFixture创建一个大于0的整数,而不是另一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望AutoFixture生成两个整数,而对于第二个整数,我不希望它为0或先前生成的数字.有没有办法告诉AutoFixture兑现要求".

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".

看着RandomNumericSequenceGenerator,我看起来像下限是1 ,因此我可能不必指定第一个要求.接下来,我正在查看播种"选项,但是如此答案所示,它将不会用于默认情况下是一个数字.

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?

推荐答案

这是使用普通AutoFixture做到这一点的一种方法:

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
}

这是使用 AutoFixture.Xunit 的一种方法:

[Theory, AutoData]
public void GenerateTwoDistinctNonZeroIntegersWithAutoFixtureXunit(
    Generator<int> generator)
{
    var numbers = generator.Where(x => x != 0).Distinct().Take(2);
    // -> 72, 117
}

这篇关于如何让AutoFixture创建一个大于0的整数,而不是另一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆