title: Methods for Adding Data to Mock DBs in C# Unit Tests
body: <p>This post is meant to be more a discussion-starter, as I am somewhat new to unit testing and TDD.  </p>
  
  <p>I am currently writing some unit tests for a .NET process that interacts with several databases, and am using mock database contexts in an attempt to cover different edge cases within my tests, verify exception handling in the program itself, among other things. That being said, some of my unit tests use valid data, while others do not.</p>
  
  <p>I am looking for feedback in terms of suggested best practices when adding valid/fake data to your mock database contexts.  I've seen people do this a number of ways (e.g. - implement repository pattern, adding mock data to .csv files and making them part of the project, etc...).</p>
  
  <p>I'm currently thinking about using a repository pattern for adding <code>Survey</code> objects to the <code>Surveys</code> table in my target DB.</p>
  
  <p>First off, I've got the interface:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public interface ISurveyRepository
  {
     IQueryable&lt;Survey&gt; SurveySeries { get; }
  }
  </code></pre>
  
  <p>This is implemented both for the mocking fake/valid data repositories as needed by unit tests
  </p>
  
  <pre><code>class FakeSurveyRepository : ISurveyRepository
  {
     private static IQueryable&lt;Survey&gt; fakeSurveySeries = new List&lt;Survey&gt; {
        new Survey { id = 1, SurveyName="NotValid1", SurveyData="&lt;data&gt;fake&lt;/data&gt;"},
        new Survey { id = 2, SurveyName="NotValid2", SurveyData="&lt;data&gt;super fake&lt;/data&gt;"},
        .........,
        new Survey {id = 10, SurveyName="NotValid10", SurveyData="&lt;data&gt;the fakest&lt;/data&gt;" }       
     }.AsQueryable();
  
     public IQueryable&lt;Survey&gt; SurveySeries 
     { 
        get { return fakeSurveySeries; }
     }
  }
  // RealSurveyRepository : ISurveyRepository is similar to this, but with "good" data
  </code></pre>
  
  <p>I then have a class to consume this data for either fake/valid data by being passed a reference to the series in the constructor:</p>
  
  <pre><code>public class SurveySeriesProcessor
  {
     private ISurveyRepository surveyRepository;
  
     public SurveySeriesProcessor( ISurveyRepository surveyRepository )
     {
         this.surveyRepository = surveyRepository;
     }
  
     public IQueryable&lt;Survey&gt; GetSurveys()
     {
        return surveyRepository.SurveySeries
     }
  } 
  </code></pre>
  
  <p>And can then approach using these objects in my tests such as:</p>
  
  <pre><code>[TestClass]
  public class SurveyTests
  {
      [TestMethod]
      WhenInvalidSurveysFound_SurveyCopierThrowsInvalidSurveyDataErrorForEach()
      {
         // create mocking DB context and add fake data
         var contextFactory = new ContextFactory( ContextType.Mocking );
         var surveySeriesProcessor = new SurveySeriesProcessor( new FakeSurveyRepository() );
  
         foreach(Survey surveyRecord in surveySeriesProcessor.GetSurveys() )
         {
            contextFactory.TargetDBContext.Surveys.AddObject( surveyRecord );
         }
         // instantiate object being tested and run it against fake test data
         var testSurveyCopier = new SurveyCopier( contextFactory );
         testSurveyCopier.Start();
         // test behavior
         List&lt;ErrorMessage&gt; errors = testSurveyCopier.ErrorMessages;
         errors.Count.ShouldEqual( surveySeriesProcessor.GetSurveys().Count );
         foreach(ErrorMessage errMsg in errors)
         {
            errMsg.ErrorCode.ShouldEqual(ErrorMessage.ErrorMessageCode.InvalidSurveyData);
         }
      }
  }
  </code></pre>
  
  <p>NOTE: I realize that in the example code provided I don't necessarily need to make the classes implementing <code>ISurveyRepository</code> return the series as an <code>IQueryable&lt;Survey&gt;</code> (they could very well be <code>List&lt;Survey&gt;</code>). However, I am going to extend the functionality of the interface and these classes in the future to filter out the fake/valid series based on certain criteria added to LINQ queries, which is why I made the repositories implement <code>IQueryable&lt;&gt;</code>. This is mock-up code designed to convey the basic principles of what I'm thinking.</p>
  
  <p>With all of this in mind, what I'm asking is: </p>
  
  <ol>
  <li>Do you have any suggestions in terms of alternative approaches I could take in such scenarios?</li>
  <li>What methods have you employed in the past, what did you like/not like about them? Which have you found were the easiest to maintain?</li>
  <li>Given what I've posted, do you notice flaws in my general approach to unit testing? Sometimes I feel as though I write unit tests that attempt to cover too much ground instead of being concise, elegant, and to-the-point.</li>
  </ol>
  
  <p>This is meant to be somewhat of an open discussion. Please keep in mind, this is the first set of unit tests I've ever written (I've read a decent amount of literature on the subject, however).</p>