문제

Let's say we have an enum type defined as:

enum Statuses
{
    Completed,
    Pending,
    NotStarted,
    Started
}

I'd like to make Autofixture create a value for me other than e.g. Pending.

So (assuming round-robin generation) I'd like to obtain:

Completed, NotStarted, Started, Completed, NotStarted, ...

도움이 되었습니까?

해결책

The easiest way to do that is with AutoFixture's Generator<T>:

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

If you only need a single value, but want to be sure that it's not Statuses.Pending, you can do this:

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

There are other ways, too, but this is the easiest for an ad-hoc query.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top