Node.JS
Node.JS

Reputation: 1592

AutoFixure, randomly choose from predefined list

I am wondering in AutoFixure, is there a way to randomly choose from the predefined list? For example, when I use fixture.Create or fixture.CreateMany, it randomly selects an object from the predefined list. I did not find anything similar from documentation and searching Stack Overflow, so I am not sure it is even possible.

Upvotes: 6

Views: 2816

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233367

You can use ElementsBuilder<T>:

[Fact]
public void Example()
{
    var fixture = new Fixture();
    fixture.Customizations.Add(
        new ElementsBuilder<MyObject>(
            new MyObject("foo"),
            new MyObject("bar"),
            new MyObject("baz")));

    var actual = fixture.Create<MyObject>();

    Assert.Contains(actual.Name, new[] { "foo", "bar", "baz" });
}

This test passes.

In your actual code base, you should package that modification in an ICustomization.

Upvotes: 15

Related Questions