title: How to write unit tests for database calls
body: <p>I'm near the beginning of a new project and (gasp!) for the first time ever I'm trying to include unit tests in a project of mine.</p>
  
  <p>I'm having trouble devising some of the unit tests themselves.  I have a few methods which have been easy enough to test (pass in two values and check for an expected output).  I've got other parts of the code which are doing more complex things like running queries against the database and I'm not sure how to test them.</p>
  
  <pre><code>public DataTable ExecuteQuery(SqlConnection ActiveConnection, string Query, SqlParameterCollection Parameters)
  {
      DataTable resultSet = new DataTable();
      SqlCommand queryCommand = new SqlCommand();
      try
      {
          queryCommand.Connection = ActiveConnection;
          queryCommand.CommandText = Query;
  
          if (Parameters != null)
          {
              foreach (SqlParameter param in Parameters)
              {
                   queryCommand.Parameters.Add(param);
              }
          }
  
          SqlDataAdapter queryDA = new SqlDataAdapter(queryCommand);
          queryDA.Fill(resultSet);
      }
      catch (Exception ex)
      {
          //TODO: Improve error handling
          Console.WriteLine(ex.Message);
      }
  
      return resultSet;
  }
  </code></pre>
  
  <p>This method essentially takes in all the necessary bits and pieces to extract some data from the database, and returns the data in a DataTable object.</p>
  
  <p>The first question is probably the most complex: What should I even test in a situation like this?</p>
  
  <p>Once that's settled comes the question of whether or not to mock out the database components or try to test against the actual DB.</p>