Moq - Update with DbEntityEntry
<p>I'm using EF6. The generated code is something like:</p>

<pre><code>public partial class MyDataContext : DbContext
{
    public MyDataContext() : base("name=mydata")
    {
    }

    public virtual DbSet&lt;Book&gt; Books { get; set; }
}
</code></pre>

<p>Then I have a generic repository like:</p>

<pre><code>public class GenericRepository&lt;TObject&gt; where TObject : class
{
    protected readonly MyDataContext Context;

    protected GenericRepository(MyDataContext context)
    {
        Context = context;
    }

    public virtual TObject Update(TObject data, int id)
    {
        if (data == null)
            return null;

        TObject obj = Context.Set&lt;TObject&gt;().Find(id);
        if (obj != null)
        {
            Context.Entry(obj).CurrentValues.SetValues(data);
            Context.SaveChanges();
        }

        return obj;
    }
}
</code></pre>

<p>Then I have a service that uses the GenericRepository to update data:</p>

<pre><code>public class MyDataService&lt;TObject&gt; where TObject : class
{
    private readonly MyDataContext context;

    public MyDataService(MyDataContext ct)
    {
        context = ct;
    }

    public TObject Update(TObject obj, int id)
    {
        var r = new GenericRepository&lt;TObject&gt;(context);
        return r.Update(obj, id);
    }
}
</code></pre>

<p>So I can update a books with something like this:</p>

<pre><code>var ds = new MyDataService&lt;Book&gt;(new MyDataContext());
var data = ds.Update(new Book { Name = "New Name" }, 1);
</code></pre>

<p>This is working fine. Next I try to use Moq to unit test the above code with something like:</p>

<pre><code>var updatedBook = new Book { Name = "Update Book Name" };

var mockSet = new Mock&lt;DbSet&lt;Book&gt;&gt;();
var mockContext = new Mock&lt;MyDataContext&gt;();
mockContext.Setup(c =&gt; c.Books).Returns(mockSet.Object);
mockContext.Setup(c =&gt; c.Set&lt;Book&gt;().Find(It.IsAny&lt;object[]&gt;()))
            .Returns&lt;object[]&gt;(ids =&gt; chips.FirstOrDefault(d =&gt; d.Id == (int)ids[0]));

var service = new MyDataService&lt;Book&gt;(mockContext.Object);
var data = service.Update(updatedBook, 1);
</code></pre>

<p>However, I get an exception on the <code>Context.Entry(obj).CurrentValues.SetValues(data)</code> line. </p>

<p>How do I mock the Update method properly?</p>