title: Mocking Objects for Unit Tests
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4370617/hard-coded-mock-objects-vs-mocking-framework">Hard-Coded Mock Objects vs Mocking Framework</a>  </p>
  </blockquote>
  
  
  
  <p>I think I'm finally beginning to understand what unit tests are intended to solve, but am still having trouble implementing all of the details. I've come to the conclusion that I may require a "mock" (and I use this term lightly since I'm not sure I need an entire framework like Moq) object to get the job done.</p>
  
  <p>As an example of the issue I have been running in to, consider an implementation of the Repository Pattern (or similar). As I currently understand, I would need (at a minimum) tests for each of the <code>Add()</code>, <code>Get()</code>, and <code>Remove()</code> class methods. This is fine, except suppose I want to test how the <code>Add()</code> method handles <code>null</code> references. In this case, would I merely define a simple class within the test project and set an instance of this to <code>null</code> within the appropriate unit test?</p>
  
  <p>Example Unit Test (Illustration):</p>
  
  <pre><code>[TestMethod]
  public void TestAdd_Null()
  {
      IRepository&lt;MockObject&gt; repository = (IRepository&lt;MockObject&gt;)(new Repository&lt;MockObject&gt;());
      MockObject testObject = null;
  
      repository.Add(testObject);
  
      Assert.IsNotNull(repository.Entity);
  }
  
  // I'm thinking I should implement something like this exclusively within the Test project.
  // Is this reasonable? Or should I be looking into something else?
  internal class MockObject
  {
      public String Name { get; set; }
  }
  </code></pre>