Mocking or faking DbEntityEntry or creating a new DbEntityEntry

后端 未结 2 636
甜味超标
甜味超标 2020-12-01 04:12

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 tha

2条回答
  •  感动是毒
    2020-12-01 04:57

    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();
    mockContext.Setup(c => c.MyDbSet).Returns(mockMyDbSet.Object);
    mockContext.Setup(c => c.SetModified(It.IsAny()));
    

提交回复
热议问题