title: Definition of Unit Test method which calls stored procedure to retrieve data
body: <p>I have following method in asp.net code behind page which calls stored procedure and return results. I am not sure about definition of unit test method i am supposed to write to test this method.</p>
  
  <pre><code>public List&lt;BlogDetail&gt; GetData()
      {
          List&lt;BlogDetail&gt; allBlogDetail = new List&lt;BlogDetail&gt;();
  
          using (var db = new BloggingContext("CodeFirstSampleConnectionString")) //name=CodeFirstSample
          {
              allBlogDetail = db.Database.SqlQuery&lt;BlogDetail&gt;("uspGetAllBlogs").ToList();
          }
  
          return allBlogDetail;
      }
  </code></pre>
  
  <p>Following is the unit test method i wrote to test this method. As far as i know, it is not a good practice to make database calls in unit test which i have violated by making a database call in unit test method. </p>
  
  <pre><code>public void GetDataTest()
      {
          Default target = new Default();
          List&lt;BlogDetail&gt; expected = new List&lt;BlogDetail&gt;();
          List&lt;BlogDetail&gt; actual = new List&lt;BlogDetail&gt;();
  
          using (var db = new BloggingContext("DemoConnectionString")) 
          {
              expected = db.Database.SqlQuery&lt;BlogDetail&gt;("uspGetAllBlogs").ToList();
          }
  
          actual = target.GetData();
  
          Assert.AreEqual(expected.Count, actual.Count);
  
          for (int i = 0; i &lt; expected.Count; i++)
          {
              Assert.AreEqual(expected[i].BlogID, actual[i].BlogID);
              Assert.AreEqual(expected[i].BlogName, actual[i].BlogName);
              Assert.AreEqual(expected[i].PostID, actual[i].PostID);
              Assert.AreEqual(expected[i].PostTitle, actual[i].PostTitle);
              Assert.AreEqual(expected[i].PostContent, actual[i].PostContent);
          }
      }
  </code></pre>
  
  <p>I have read that unit test should be repeatable and predictable and by writing above method i have violated both the rules. </p>
  
  <p>This unit test takes at at least 30 seconds to run in a local database and apparently it will take more time when tested with remote database.</p>
  
  <p>Result of stored procedure is also not predictable as stored procedure can return variable records when called at different times. Since database is fed with data at regular interval.</p>
  
  <p>I have no idea about definition of unit test method. Is my unit test method correct? Do i need to take different approach? Please help.</p>