自动修复,从预定义列表中随机选择

Nod*_*.JS 3 c# autofixture

我想知道在AutoFixure中,是否可以从预定义列表中随机选择?例如,当我使用fixture.Create或时fixture.CreateMany,它将从预定义列表中随机选择一个对象。我从文档和搜索Stack Overflow中都没有找到任何类似的东西,所以我不确定是否有可能。

Mar*_*ann 5

您可以使用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" });
}
Run Code Online (Sandbox Code Playgroud)

该测试通过。

在您的实际代码库中,您应该将该修改打包在中ICustomization