38

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 ?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Martin
  • 23,844
  • 55
  • 201
  • 327

1 Answers1

50

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()
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Nikos Baxevanis
  • 10,868
  • 2
  • 46
  • 80
  • 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 again – Martin Jul 01 '14 at 10:53
  • 12
    Use `CreateMany` then, e.g. `fixture.CreateMany(9).ToArray()`. – Nikos Baxevanis Jul 01 '14 at 11:29
  • Is there any parameter attribute if I want to use Theory and AutoData test method attributes? – Alexey Zimarev Jan 28 '16 at 11:31
  • What do you mean by *any parameter attribute*? – Nikos Baxevanis Jan 28 '16 at 14:25
  • 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. – Ryan May 24 '17 at 13:08
  • 1
    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. – Nikos Baxevanis May 25 '17 at 02:43
  • Here's an example of how to write such an attribute: https://stackoverflow.com/a/67713352/6772415 – nicstella May 27 '21 at 02:22
  • @nicstella, very nice! – Nikos Baxevanis Jun 06 '22 at 09:15