c# - 从所有值的子集中创建匿名枚举值

标签 c# autofixture

假设我们有一个枚举类型定义为:

enum Statuses
{
    Completed,
    Pending,
    NotStarted,
    Started
}

我想让 Autofixture 为我创造一个值(value),而不是例如。待定。

所以(假设循环生成)我想获得:

已完成,未开始,已开始,已完成,未开始,...

最佳答案

最简单的方法是使用 AutoFixture 的 Generator<T> :

var statuses = fixture
    .Create<Generator<Statuses>>()
    .Where(s => Statuses.Pending != s)
    .Take(10);

如果您只需要一个值,但想确定它不是 Statuses.Pending ,你可以这样做:

var status = fixture
    .Create<Generator<Statuses>>()
    .Where(s => Statuses.Pending != s)
    .First();

还有其他方法,但这对于临时查询来说是最简单的。

关于c# - 从所有值的子集中创建匿名枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20957010/

相关文章:

moq - 处理 AutoFixture 和 Moq 之间的样本创建不一致

c# - 使用 AutoMoq 创建 Controller 时,Fixture.CreateAnonymous 方法会因错误 (AutoFixture) 终止测试运行程序进程

c# - 如何在带有cordova的windows phone 8上使用现有的sqlite数据库

c# - 为什么 WebClient.OpenRead 在 Windows Server 2008 R2 上超时

c# - LibLog 中的上下文日志记录

c# - 在C#或vb.net中使用wmlib.dll刻录CD音频而没有gab

c# - 如何配置 AutoFixture 在创建许多特定类型时使用枚举值作为种子?

c# - 泛型类的依赖注入(inject)

c# - 为什么 AutoFixture 不能创建这个类?

AutoFixture + NSubstitute 为注入(inject)的自动模拟抛出 NotASubstituteException