title: Unit Test a method that searches element in db
body: <p>I wanted to do a Unit Test for a method in the following Class:</p>
  
  <pre><code> public class SearchInfo : IDisposable
          {
              public List&lt;SearchResult&gt; SerialResults { get; set; }
              public List&lt;SearchResult&gt; TagResults { get; set; } 
              /// &lt;summary&gt;
              /// Uses the Search string to filter a resultset by Serial and Tag
              /// &lt;/summary&gt;
              /// &lt;param name="search"&gt;&lt;/param&gt;
              public SearchInfo(string search)
              {
                  SerialResults = new List&lt;SearchResult&gt;();
                  TagResults = new List&lt;SearchResult&gt;();
  
                  SerialResults.AddRange(FindSerial(search));
                  TagResults.AddRange(FindTags(search));
  
              }
  
              private static IEnumerable&lt;SearchResult&gt; FindTags(string search)
              {
  
                  List&lt;SearchResult&gt; result;
                  using (var db = new TIPWebITDataContext())
                  {
                      result = (from i in db.tblTechInventories
                                where i.Tag.Equals(search)
                                select new SearchResult()
                                    {
                                        SearchType = "Tag",
                                        Key = i.Tag,
                                        KeyCaption = i.Tag,
                                        Name = i.tblTechItem.ItemName
  
                                    }).ToList();
                  }
                  return result;
              }
              private static IEnumerable&lt;SearchResult&gt; FindSerial(string search)
              {
                  List&lt;SearchResult&gt; result;
                  using (var db = new TIPWebITDataContext())
                  {
                      result = (from i in db.tblTechInventories
                                where i.Serial.Contains(search)
                                select new SearchResult()
                                {
                                    SearchType = "Searial",
                                    Key = i.Tag,
                                    KeyCaption = i.Serial,
                                    Name = i.tblTechItem.ItemName
  
                                }).ToList();
                  }
                  return result;
  
              }
  
              #region Implementation of IDisposable
  
              public void Dispose()
              {
                  SerialResults = null;
                  TagResults = null;
              }
  
              #endregion
          }
  
          public class SearchResult
          {
              public string SearchType { get; set; }
              public string Key { get; set; }
              public string KeyCaption { get; set; }
              public string Name { get; set; }
          }
  
  
      }
  </code></pre>
  
  <p>Note:</p>
  
  <p>Method <code>Name = FindTags()</code> returns return result.
  The method checks the entered tag is present in database or not. If it is available returns the result.</p>
  
  <p>How will we unit test this method?</p>
  
  <p>Have mocked the Repo
  Have covered scenario of "THROWS EXCEPTION"
  Question is how to unit test the scenario of ALL WENT WELL ie user;s makeModel matched with repository's makeModel</p>