title: DbSet Find with Moq (EntityFramework.Testing.Moq)
body: <p>I've been using the Moq objects along with <a href="https://github.com/scott-xu/EntityFramework.Testing#entityframeworktestingmoq--" rel="noreferrer">EntityFramework.Testing.Moq</a> extension and I recently hit a bump trying to do a Find.  What I had thought was right, that simply doing a setup like this (from the documentation):</p>
  
  <pre><code>// Create some test data
  var data = new List&lt;Blog&gt;
  {
      new Blog{ Id = 1, Name = "BBB" },
      new Blog{ Id = 2, Name = "CCC" },
      new Blog{ Id = 3, Name = "AAA" }
  };
  
  // Create a mock set and context
  var set = new Mock&lt;DbSet&lt;Blog&gt;&gt;()
      .SetupData(data);
  
  var context = new Mock&lt;BloggingContext&gt;();
  context.Setup(c =&gt; c.Blogs).Returns(set.Object);
  
  // Create a BlogsController and invoke the Index action
  var controller = new BlogsController(context.Object);
  var result = await controller.Index();
  </code></pre>
  
  <p>In the controller, I could do this (again, from the doc):</p>
  
  <pre><code>var query = db.Blogs.OrderBy(b =&gt; b.Name);
  </code></pre>
  
  <p>But, then when I tried to do a DbSet Find, like this using the model's key:</p>
  
  <pre><code>var b = db.Blogs.Find(1);
  </code></pre>
  
  <p>I would get back null.</p>
  
  <p>Now, I've been able to make this work by doing my setup like this:</p>
  
  <pre><code>context.Setup(m =&gt; m.Blogs.Find(It.IsAny&lt;int&gt;()))
     .Returns&lt;object[]&gt;(s =&gt; data.Find(d =&gt; d.Blogs == (int)s[0]));
  </code></pre>
  
  <p>but I'm left wondering if I was doing something wrong in the first place and if I am over complicating things somehow.</p>
  
  <p>Thanks for any suggestions!</p>