Watson Jon

Code and ramblings from Watson
posts - 15, comments - 7, trackbacks - 0

My Links

News

Twitter












Archives

Post Categories

User Groups

Go ahead and repeat yourself

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!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Monday, October 05, 2009 4:13 PM |

Feedback

Gravatar

# re: Go ahead and repeat yourself

can the searchText variable be replaced with a random string generator? Or is the SearchResult constructor only called once?
10/5/2009 9:22 PM | PJack
Gravatar

# re: Go ahead and repeat yourself

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.
10/6/2009 9:41 AM | WatsonJon
Gravatar

# re: Go ahead and repeat yourself

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!


10/6/2009 9:53 AM | WatsonJon
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: