title: How to mock an object that does a complicated entity framework LINQ query?
body: <p>I'm trying to write a unit test for a simple mvc controller that makes a complicated LINQ query into the database:</p>
  
  <pre><code>public class HomeController
  {
      private readonly DamagesDbContext db;
  
      public HomeController(DamagesDbContext db)
      {
          this.db = db;
      }
  
      // GET: /Home/
      [Authorize]
      public ActionResult Index()
      {
          var dashData = (from inc_c in db.incident_content
                         join inc in db.incidents
                         on inc_c.incidentid equals inc.incidentid
                         where inc.currentrevisionnumber == inc_c.revisionnumber
                         group inc_c by 1 into g
                         select new{
                             total = g.Count(),
                             open = g.Count(q =&gt; q.incidentstatus == "OPEN"),
                             closed = g.Count(q =&gt; q.incidentstatus == "CLOSED")
                         }).SingleOrDefault();
  
          ViewBag.total = dashData.total;
          ViewBag.open = dashData.open;
          ViewBag.closed = dashData.closed;            
  
          return View();
      }
  }
  </code></pre>
  
  <p>And then in my test, I have:</p>
  
  <pre><code>var mockDb = new Mock&lt;DamagesDbContext&gt;();
  mockDb.Setup(/* What goes here? */);
  
  var homeController = new HomeController(mockDb.Object);
  
  var result = homeController.Index();
  
  // Various asserts go here...
  </code></pre>
  
  <p>But what do I do in Setup(), to wire up a replacement for that complicated LINQ query?</p>
  
  <p>How do I know what actual methods are being called?  Or what their arguments are?</p>
  
  <p>==== EDITED ====</p>
  
  <p>I think that part of my problem is that the LINQ expression, while kinda neat, doesn't make it clear what methods are being called on what objects.</p>
  
  <p>I'm playing around with Resharper, for the first time, and I just noticed that it has a "convert LINQ to method chain" option.  With that done, the above LINQ expression turns into:</p>
  
  <pre><code>var dashData = (this.db.incident_content.Join(this.db.incidents, inc_c =&gt; inc_c.incidentid,
      inc =&gt; inc.incidentid, (inc_c, inc) =&gt; new {inc_c, inc})
      .Where(@t =&gt; @t.inc.currentrevisionnumber == @t.inc_c.revisionnumber)
      .GroupBy(@t =&gt; 1, @t =&gt; @t.inc_c)
      .Select(g =&gt; new
      {
          total = g.Count(),
          open = g.Count(q =&gt; q.incidentstatus == "OPEN"),
          closed = g.Count(q =&gt; q.incidentstatus == "CLOSED")
      })).SingleOrDefault();
  </code></pre>
  
  <p>Which might make it a bit clearer as to what objects and methods need to be mocked.</p>