Specify desired amount of items when configuring a customization
See original GitHub issueHi,
I’m trying to test a piece of code that is sensible to the amount of items in a given set. Unluckily, I really can’t avoid passing proper data to activate a specific path.
I would normally use CreateMany<T>(int)
to specify the amount of items I need, but the set is in a property of a container object (btw, I am addicted to the new relay for IReadOnlyList<T>
) like
public class ContactIdList
{
[JsonProperty("vids")]
public IReadOnlyList<long> ContactIds { get; set; }
[JsonProperty("vidOffset")]
public long? Offset { get; set; }
[JsonProperty("hasMore")]
public bool HasMore { get; set; }
}
What I would suggest is to offer a way to specify the amount of items needed to be generated when customizing a type.
Ideally, something like this
var list = fixture.Build<ContactIdList>()
.With(p => p.HasMore, false)
.Without(p => p.Offset)
.WithMany(p => p.ContactIds, 16)
.Create();
The parameters should be something like (Expression<Func<T, IEnumerable<TProperty>> propertyPicker, int count)
but I’m honestly torn between naming it WithMany<TProperty>
and adding another overload of With<TProperty>
.
Obviously, I’m aware that I can create the list before and use With<TProperty>(Expression<Func<T, TProperty> propertyPicker, TProperty value)
to pass the list.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
Agreed 😉 Converting this to a potential feature.
Thanks for raising the scenario.
I do understand the scenario you have, however I don’t like too much the suggested way to fix it. The scenario looks very specific to extend API for it.
Instead, it’s much better to implement it as a part of the #708. If we add the
With()
method overload taking the factory method, so it can be solved like this:This solution is way more generic as behavior is not limited to collections only.
@moodmosaic I’d be really happy to implement #708 as it’s a real enabler for more specific scenario. Therefore, if you accept such a PR, I could implement it 😉