title: How to write integration and system tests in Asp.net MVC
body: <h2>My application</h2>
  
  <p>I have an application design that looks like this:</p>
  
  <ul>
  <li>web application layer - asp.net MVC app with controllers and views that use POCOs and call services</li>
  <li>service layer - business processes that use POCOs and call repositories</li>
  <li>data layer - repositories that use POCOs and communicate with the model in the form of EF model which is part of this same layer</li>
  <li>POCO layer - defines all classes that are used for inter communication between these layers</li>
  </ul>
  
  <p>So my data layer is completely transparent to data model implementation because upper layer don't use data entities at all.</p>
  
  <h2>Testing</h2>
  
  <p>As much as I understand unit, integration and system testing (with relation to Asp.net MVC) is this:</p>
  
  <ul>
  <li>unit testing - this one's easy. Mock decoupled objects and inject them in your unit test so tested unit will use them</li>
  <li>integration testing - should make a group of production functionality units and mock the rest: so writing integration tests that would test controller, service and repository integration without actually using production database</li>
  <li>system testing - run tests on all layers without any mocking, meaning I have to use production (test) database as well</li>
  </ul>
  
  <h2>Problem</h2>
  
  <p>I can easily see how to write unit tests as well as system tests, but I don't know how to write integration tests? Maybe my view of these is completely distorted and I don't understand them at all.</p>
  
  <p><strong>How should one write integration and system tests for an Asp.net MVC application?</strong><br>
  Or any .net application for that matter?</p>
  
  <h2>Some code that helps explain the problem</h2>
  
  <p>Suppose I have classes like:</p>
  
  <ul>
  <li><code>TaskController</code> calls into <code>TaskService</code></li>
  <li><code>TaskService</code> calls into <code>TaskRepository</code></li>
  <li><code>TaskRepository</code> manipulate EF data internally</li>
  </ul>
  
  <p>So here are my (abbreviated) classes:</p>
  
  <pre><code>public class TaskController
  {
      private ITaskService service;
  
      // injection constructor
      public TaskController(ITaskService service)
      {
          this.service = service;
      }
  
      // default constructor
      public TaskController() : this(new TaskService()) {}
  
      public ActionResult GetTasks()
      {
          return View(this.service.GetTasks());
      }
      ...
  }
  
  public class TaskService : ITaskService
  {
      private ITaskRepository repository;
  
      // injection constructor
      public TaskService(ITaskRepository repository)
      {
          this.repository = repository;
      }
  
      // default constructor
      public TaskService() : this(new TaskRepository()) {}
  
      public IList&lt;Task&gt; GetTasks()
      {
          return this.repository.GetTasks();
      }
      ...
  }
  
  public class TaskRepository : ITaskRepository
  {
      public IList&lt;Task&gt; GetTasks()
      {
          // code that gets tasks from EF and converts to Task POCOs
      }
      ...
  }
  </code></pre>
  
  <p>Unit test is simple and would look like this:</p>
  
  <pre><code>public void UnitTest()
  {
      var mock = new Mock&lt;ITaskService&gt;();
      // other code that mocks the service
  
      TaskController controller = new TaskController(mock.Object);
  
      // do the test
  }
  </code></pre>
  
  <p>But when it comes to an integration test, how do I mock only certain parts of the integration.</p>
  
  <pre><code>public void IntegrationTest()
  {
      // no mocking at all
      TaskController = new TaskController();
      // do some testing
  }
  </code></pre>
  
  <p>First of all I can't just mock database here? I could mock repository and have real service and controller though...</p>