14

pretty simple question: can I use NBuilder to create a collection of x number of random strings?

I was trying...

// NOTE: Tags need to be lowercase.
return Builder<string>
    .CreateListOfSize(10)
    .WhereAll()
        .Has(x => x = randomGenerator.Phrase(15))
    .WhereTheFirst(1)
        .Has(x => x = "time")
    .AndTheNext(1)
        .Has(x => x = "place")
    .AndTheNext(1)
        .Has(x => x = "colour")
    .Build();

but it was run-time erroring, something about I needed to call some specific constructor or something.

Anyone have any ideas?

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

2 Answers2

15

Sorry about bringing an old thread back to life, but I just wanted to share this solution/hack:

var myList = Enumerable.Range(0, 10).Select(el => generator.Phrase(10));

Your feedback is appreciated :)

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
Andrea Scarcella
  • 3,233
  • 2
  • 22
  • 26
9

NBuilder creates objects by using the default (parameterless) constructor. The exception you are receiving is because the String class doesn't have a default constructor.

To create a list of random strings, you could use the Phrase method inside a loop. Maybe not as clean as a single NBuilder chain, but it gets the job done:

   List<string> stringsList = new List<string>();
   var generator = new RandomGenerator();
   for (int i = 0; i < 10; i++)
   {
       stringsList.Add(generator.Phrase(15));
   }

   return stringsList;
Pedro
  • 12,032
  • 4
  • 32
  • 45
  • so then NBuilder can't create random strings? – Pure.Krome Feb 01 '11 at 06:34
  • 1
    A random string - yes. A list of random strings using a single NBuilder chain of methods - not that I am aware of. I've updated my answer to include a way of generating the list. – Pedro Feb 01 '11 at 15:46