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!