简体   繁体   中英

Implementing Unit Testing with EF Entry?

I'm having a problem implementing unit testing in Entity Framework Code First. I've been using this Microsoft blog http://msdn.microsoft.com/en-us/data/dn314431.aspx as a guide, and I have managed to implement everything perfectly and have operations (insert, query) that are operating as specified in the examples. I have a problem , however, with the update operation. My method contains the following line:

ctx.Entry(ws).Property(w => w.IsDeleted).IsModified = true;

This is responsible for marking a property as modified. The problem is that my context does not support the implementation of the Entry method that is part of DbEntityEntry, and does not know how to simulate the update on my testing methods.

I am using Rhino Mocks for my tests.

Struggled with that problem a bit. The best solution for me was rising a level of indirection (answer from this thread on SO ). The idea is following:

ctx.Entry(ws).Property(w => w.IsDeleted).IsModified = true;

is transformed to calls like:

ctx.SetModified(ws);

...
// Real context implementation
public void SetModified(object entity)
{
    this.Entry(entity).State = System.Data.Entity.EntityState.Modified;
}

and you can do anything you need to "update" entity in your mock implementation. Wish you luck unit-testing EF code ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM