问题
This seems to be something lacking in every blog post of piece of documentation I read.
How can I CreateMany<T>
where only 1 of the elements in the collection contains an Id
of something I specify?
I want to pull this back into a customization.
I tried many variations of something like this:
public class OrdersCustomizations : ICustomization
{
public void Customize(IFixture fixture)
{
var single = fixture.Build<Order>().With(x => x.Id, 10).Create();
var many = fixture.Build<Order>().CreateMany().ToList();
many.Add(single);
fixture.RepeatCount = 5;
fixture.AddManyTo<Order>(many);
}
}
But I can't figure this out.
回答1:
Why don't you just do the following?
var many = fixture.Create<List<Order>>();
many[0].Id = 10;
// Rest of test goes here...
Or, if you're using the declarative approach with AutoFixture.Xunit, you can do this:
public void MyTest(List<Order> orders)
{
orders[0].Id = 10;
// Rest of test goes here...
}
回答2:
// Configure fixture
var expectedResult = _fixture.Create<List<Order>>();
expectedResult[0].Id = "159";
_fixture.Inject(expectedResult);
// Usage
var actualResult = _fixture.Create<List<Order>>();
Assert.True(actualResult[0].Id == expectedResult[0].Id);
This is what you are searching!
来源:https://stackoverflow.com/questions/29270580/createmany-where-only-1-element-contains-certain-value