title: Stubbing / mocking a database in .Net
body: <p>I have a webservice which basically just executes some stored procedures, transforms the data and sends it to the browser. No fancy ORM mapper or something like that involved. To be able to write test without accessing the database, I have done the following:</p>
  
  <ul>
  <li>I have extracted all calls to the DB into one class. The methods return just the DataSet and DataTable objects.</li>
  <li>Executed a sample call for each method and serialized the DataSet/DataTable to disk.</li>
  <li>Extracted an interface exposing all available methods.</li>
  <li>Implemented a fake database class which just loads the serialized data and returns it.</li>
  </ul>
  
  <p>Now I have serialized sample results which I can check in with my project, and I can use the fake database in my tests.</p>
  
  <p>This works quite well for me. Is there some framework which makes creating and loading the sample data easier? My current project is small, but I would use the same schema in larger projects.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Obviously all answers are not wrong, but miss the point. I'm aware of the basics of unit testing. But my code is working with DataTables, so I would have to somehow fake my DataTables. Building a DataTable from scratch is not an easy task, and it would bloat my tests and reduce readability. In my case, it would be quite impossible to generate useful sample data by hand.</p>
  
  <p>Therefore, I executed some sample calls against a sample database to get some DataTables. I have serialized these tables to disk and use the serialized versions to create my fake DataTables when testing. That way the tests are independent of the database.</p>
  
  <p>There are different options regarding how to structure the code, to make deserialization of the tables easier. But those are implementation details which don't need a discussion at this point. My problem is the following:</p>
  
  <p>Managing the sample calls and (de)serializing the tables is tedious work. I was looking for some tools to make this easier.</p>