Mocking or faking DbEntityEntry or creating a new DbEntityEntry

Entity FrameworkEf Code-First

Entity Framework Problem Overview


Following on the heels of my other question about mocking DbContext.Set I've got another question about mocking EF Code First.

I now have a method for my update that looks like:

if (entity == null)
    throw new ArgumentNullException("entity");

Context.GetIDbSet<T>().Attach(entity);
Context.Entry(entity).State = EntityState.Modified;
Context.CommitChanges();

return entity;

Context is an interface of my own DbContext.

The problem I'm running in to is, how do I handle the

Context.Entry(entity).State.

I've stepped through this code and it works when I have a real live DbContext as the implementation of my Context interface. But when I put my fake context there, I don't know how to handle it.

There is no constructor for a DbEntityEntry class, so I can't just create a new one in my fake context.

Has anyone had any success with either mocking or faking DbEntityEntry in your CodeFirst solutions?

Or is there a better way to handle the state changes?

Entity Framework Solutions


Solution 1 - Entity Framework

Just like the other case, what you need is to add an additional level of indirection:

interface ISalesContext
{
    IDbSet<T> GetIDbSet<T>();
    void SetModified(object entity)
}

class SalesContext : DbContext, ISalesContext
{
    public IDbSet<T> GetIDbSet<T>()
    {
        return Set<T>();
    }

    public void SetModified(object entity)
    {
        Entry(entity).State = EntityState.Modified;
    }
}

So, instead of calling the implementation, you just call SetModified.

Solution 2 - Entity Framework

Found this question when I needed to unit test with Moq, no need for your own interface. I wanted to set specific fields to not modified but the method SetModified can be used with object as well.

DbContext:

public class AppDbContext : DbContext
{	
    ...
    public virtual void SetModified(GuidEntityBase entity)
    {
        Entry(entity).State = EntityState.Modified;
        Entry(entity).Property(x => x.CreatedDate).IsModified = false;
        Entry(entity).Property(x => x.CreatedBy).IsModified = false;
    }
	...
}

Test:

var mockContext = new Mock<AppDbContext>();
mockContext.Setup(c => c.MyDbSet).Returns(mockMyDbSet.Object);
mockContext.Setup(c => c.SetModified(It.IsAny<GuidEntityBase>()));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontaylonrView Question on Stackoverflow
Solution 1 - Entity FrameworkDiego MijelshonView Answer on Stackoverflow
Solution 2 - Entity FrameworkOgglasView Answer on Stackoverflow