title: Testing Linq Queries EF and the Repository Pattern
body: <p>Trust me, I've read many articles and questions here on SO. But I haven't found a satisfactory answer.
  Take <a href="http://moleseyhill.com/blog/2009/06/29/simple-repository-pattern/" rel="nofollow">Matt Robert's Tutorial</a> on the repository pattern and unit testing as an example:</p>
  
  <p>I don't get it.. do both repositories contain the exact same query logic? It appears to be so, and if so how is that a useful pattern? It would mean whenever I change the query in the fake repository, I have to change the query logic in the actual repository - that sounds like a PITA and recipe for problems. </p>
  
  <p>I would like to keep the query in <em>one</em> method and test that method. It later occurred to me, I could do like this: </p>
  
  <pre><code>//Student can be from the db or a fake list
  public person GetStudent(IEnumerable&lt;Student&gt; students,int studentid)
  {
     return students.FirstOrDefault(s =&gt;s.PersonId ==studentid);
  }
  
  //unit test
      Assert.IsNull(GetStudent(fakeStudentList, -1))
  //actual code
      var student =  GetStudent(entities.students,-1) 
  </code></pre>
  
  <p>Is there a pattern that mimics above? If so, what is it? Or is that in fact how the repository pattern works? If so, why does it feel like the repository pattern duplicates code?</p>
  
  <p>And also, the <em>effort</em>.. look at this terrifying <strong><a href="http://mylifeandcode.blogspot.ie/2012/10/unit-testing-with-mock-entity-framework.html" rel="nofollow">example</a></strong> :S?</p>