AutoFixure,从预定义列表中随机选择
AutoFixure, randomly choose from predefined list
我想知道在AutoFixure中,有没有办法从预定义列表中随机选择?例如,当我使用 fixture.Create
或 fixture.CreateMany
时,它会从预定义列表中随机选择一个对象。我没有从 documentation 和搜索 Stack Overflow 中找到任何类似的东西,所以我不确定它是否可能。
您可以使用 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" });
}
此测试通过。
在您的实际代码库中,您应该将该修改打包到 ICustomization
。
我想知道在AutoFixure中,有没有办法从预定义列表中随机选择?例如,当我使用 fixture.Create
或 fixture.CreateMany
时,它会从预定义列表中随机选择一个对象。我没有从 documentation 和搜索 Stack Overflow 中找到任何类似的东西,所以我不确定它是否可能。
您可以使用 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" });
}
此测试通过。
在您的实际代码库中,您应该将该修改打包到 ICustomization
。