title: asp.net mvc - How to create fake test objects quickly and efficiently
body: <p>I'm currently testing the controller in my mvc app and I'm creating a fake repository for testing. However I seem to be writing more code and spending more time for the fakes than I do on the actual repositories. Is this right?</p>
  
  <p>The code I have is as follows:</p>
  
  <p>Controller</p>
  
  <pre><code>public partial class SomeController : Controller
  {
      IRepository repository;
  
      public SomeController(IRepository rep)
      {
          repository = rep;
      }
  
      public virtaul ActionResult Index()
      {
          // Some logic
          var model = repository.GetSomething();
  
          return View(model);
      }
  }
  </code></pre>
  
  <p>IRepository</p>
  
  <pre><code>public interface IRepository
  {
      Something GetSomething();
  }
  </code></pre>
  
  <p>Fake Repository</p>
  
  <pre><code>public class FakeRepository : IRepository
  {
      private List&lt;Something&gt; somethingList;
  
      public FakeRepository(List&lt;Something&gt; somethings)
      {
          somthingList = somthings;
      }
  
      public Something GetSomething()
      {
          return somethingList;
      }
  }
  </code></pre>
  
  <p>Fake Data</p>
  
  <pre><code>class FakeSomethingData
  {
      public static List&lt;Something&gt; CreateSomethingData()
      {
          var somethings = new List&lt;Something&gt;();
  
          for (int i = 0; i &lt; 100; i++)
          {
              somethings.Add(new Something
              {
                  value1 = String.Format("value{0}", i),
                  value2 = String.Format("value{0}", i),
                  value3 = String.Format("value{0}", i)
              });
          }
  
          return somethings;
      }
  }
  </code></pre>
  
  <p>Actual Test</p>
  
  <pre><code>[TestClass]
  public class SomethingControllerTest
  {
      SomethingController CreateSomethingController()
      {
          var testData = FakeSomethingData.CreateSomethingData();
          var repository = new FakeSomethingRepository(testData);
  
          SomethingController controller = new SomethingController(repository);
  
          return controller;
      }
  
      [TestMethod]
      public void SomeTest()
      {
          // Arrange
          var controller = CreateSomethingController();
  
          // Act
          // Some test here
  
          // Arrange
      }
  }
  </code></pre>
  
  <p>All this seems to be a lot of extra code, especially as I have more than one repository. Is there a more efficient way of doing this? Maybe using mocks?</p>
  
  <p>Thanks</p>