Reputation: 557
Is there a way to know the RepeatCount from a ISpecimenBuilder? I am creating a new specimen builder for certain parameter name. Basically the Create method will ask for the parameter name and in case it is e.g. "myParamName", and the type is e.g. "MyParamType" it will return a customized object.
This works perfect, however, if the parameter type is IEnumerable{MyParamType} I would like to "CreateMany". The problem is...how many?
I cannot call context.Resolve or context.CreateMany{MyParamType} for the simple reason that it would not create my objects the way I need. Example: if MyParamType is string, it would not create the formatted string I need. That is exactly why I created a specimen builder in the first place!:) I also cannot register my new specimen builder for MyParamType as I only need this customization for parameter value requests of a specific name. Otherwise, I need the default behavior
Thank you in advance
Upvotes: 2
Views: 1678
Reputation: 59983
I'm not entirely sure if this is what you're trying to do, but you don't have to worry about handling requests for IEnumerable<T>
in your custom specimen builder; AutoFixture will automatically convert requests for IEnumerable<T>
into multiple requests for T
, which is then handled by your specimen builder.
For example, let's assume we have a custom specimen builder that simply return the value "foo"
for all requests for string
:
public class FooBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
return request.Equals(typeof(string))
? (object)"foo"
: new NoSpecimen();
}
}
Now, this test passes:
[Fact]
public void Should_return_all_foos_for_a_sequence_of_strings()
{
var fixture = new Fixture { RepeatCount = 4 };
fixture.Customizations.Insert(0, new FooBuilder());
var many = fixture.Create<IEnumerable<string>>();
Assert.Equal(4, many.Count());
Assert.All(many, i => Assert.Equal("foo", i));
}
because AutoFixture makes sure to invoke the FooBuilder
as many times as it's needed in order to satisfy the request.
Upvotes: 1
Reputation: 2708
The problem is...how many?
By default, that's 3. You may change this parameter when creating a new Fixture
instance:
var fixture = new Fixture { RepeatCount = 4 };
Upvotes: 0