1

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.

user3666197
  • 1
  • 6
  • 50
  • 92
  • Perhaps you need to cast ctx to DbEntityEntry? Like so: ((DbEntityEntry)ctx).Entry(ws)...etc. – Chris B. Behrens Sep 24 '14 at 21:44
  • My problem is implementing in a new Context – Raul Molina Sep 24 '14 at 21:58
  • Welcome to SO. Instead of requiring people to read the linked tutorial, it is better to paste the relevant information from the link into your question. Otherwise, the question doesn't make sense unless someone clicks through and finds all of the relevant information themselves. You'll have better luck making it easier. – Keith Payne Sep 24 '14 at 22:11

1 Answers1

2

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 ;)

Community
  • 1
  • 1
BZ_M97
  • 186
  • 1
  • 6