Geeks With Blogs
Watson Jon Code and ramblings from Watson

Did you know about the “Repeat” extension method? Enumerable.Repeat generates a sequence of repeated values – perfect for some testing scenarios.

Here’s how I’ve used it…

private static ISearchService CreateSearchServiceWithExpectedResults(string searchText, int ountOfResults)
{
     var results = Enumerable.Repeat(new SearchResult
                                                              {
                                                                  Id = searchText,
                                                                  Description = searchText,
                                                                  Title = searchText,
                                                                  Project = searchText
                                                              }, countOfResults);

     return new MockSearchService(results.ToList());
}

 

 

This method takes in a string and sets it to some values of an object, SearchResult, and then creates ‘countOfResults’ copies of that object. A nice, quick way to get some test data. Now I can easily ask for 1, 10, or 100 values to fill my list for testing. Give it a whack and see if it helps your test writing!

Posted on Monday, October 5, 2009 4:13 PM | Back to top


Comments on this post: Go ahead and repeat yourself

# re: Go ahead and repeat yourself
Requesting Gravatar...
can the searchText variable be replaced with a random string generator? Or is the SearchResult constructor only called once?
Left by PJack on Oct 05, 2009 9:22 PM

# re: Go ahead and repeat yourself
Requesting Gravatar...
The SearchResult ctor will only get called once then shallow copies of that object will be created. You'd have to devise another scheme to create copies of objects with random data each time.
Left by WatsonJon on Oct 06, 2009 9:41 AM

# re: Go ahead and repeat yourself
Requesting Gravatar...
Here's one way to do it, but not using the Enumerable.Repeat function. Create another method like this:

private static ISearchService CreateSearchServiceWithExpectedResults(StringGenerator stringGenerator,
int countOfResults)
{
var ints = new int[countOfResults];
var results = from i in ints
select new SearchResult
{
Id = stringGenerator.GenerateString(),
Description = stringGenerator.GenerateString(),
Title = stringGenerator.GenerateString(),
Project = stringGenerator.GenerateString()
};

return new MockSearchService(results.ToList());
}

Then create a StringGenerator class similar to this:

public class StringGenerator
{
public string Value
{
get { return GenerateString(); }
}

public string GenerateString()
{
return Guid.NewGuid().ToString();
}

public static implicit operator string(StringGenerator item)
{
return item.Value;
}
}

Now the SearchResult ctor will get called as many times as you ask and new, random string values will get created for each call to StringGenerator.GenerateString().

Hope this helps!


Left by WatsonJon on Oct 06, 2009 9:53 AM

Your comment:
 (will show your gravatar)


Copyright © Jon Dalberg | Powered by: GeeksWithBlogs.net