Autofixture: Controlling number of elements that are created of type string[]
Asked Answered
L

1

40

I have an issue with creating a string array of type string[], everytime it creates 3 values but i want to be able to control this.

I am using

 var tst = fixture.Create<string[]>();

I also looked into using CreateMany but that seemed to return a type of IEnumerable.

Anyone have any ideas ?

Loudspeaker answered 1/7, 2014 at 9:47 Comment(0)
T
52

Use the RepeatCount property:

var fixture = new Fixture { RepeatCount = 9 };
var actual = fixture.Create<string[]>();
// -> The 'actual' array is 9 items now.

or

fixture.CreateMany<string>(9).ToArray()
Thistly answered 1/7, 2014 at 10:25 Comment(10)
Great, thanks, but can I control this on each create? For example, if I need repeat = 1 and one create and repeat = 9 and another? Thanks againLoudspeaker
Use CreateMany then, e.g. fixture.CreateMany<string>(9).ToArray().Thistly
Is there any parameter attribute if I want to use Theory and AutoData test method attributes?Mirabelle
What do you mean by any parameter attribute?Thistly
He means that when using AutoData instead of newing up our test data, can we attribute the parameters to define the number of elements in the sequence? As in, you use [Frozen] to freeze a parameter and [NoAutoProperties] to... well you get the idea. I would also like to know the answer to this.Theravada
Oh, now I see. Thanks for explaining this to me. I think currently there isn't such an attribute, but you can ask for an IFixture instance through the parameters of a (parameterized) test which is decorated with one of those attributes.Thistly
Here's an example of how to write such an attribute: https://mcmap.net/q/408626/-collection-size-from-attribute-for-autofixture-declarative-autodata-parameterHydria
@nicstella, very nice!Thistly
Is the second (CreateMany) guaranteed to be all unique values like Create is supposed to?Camara
it looks like it does.Camara

© 2022 - 2024 — McMap. All rights reserved.