AutoFixure, randomly choose from predefined list

…衆ロ難τιáo~ 提交于 2019-12-10 19:06:33

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/47761145/autofixure-randomly-choose-from-predefined-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!