Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fake DbContext.Entry method in Entity Framework with repository pattern

Because I want to unit test my code I have implemented the repository pattern in my MVC4 application. I managed to make a Context Interface, a fake Context and use a fake implementation of a System.Data.Entity.DbSet by following this code.

Unfortunately, just like two posters before me (here and here), I do not manage to mock the DbContext.Entry method. I use this method for updating database entries in my code as follows:

DbContext.Entry(order).State = EntityState.Modified; 

I have not found a solution to this problem, only people who say things like:

"and what is the point of unit testing this code? You fake the Find method, then you fake DbEntityEntry and there will be no real logic to test."

or to

read this and all linked questions before you continue. (...) If you want to test your repositories create integration tests talking to the real database.

That is all good and well but still no answer to the question. I read the critique and I still do want this Entry method so I will be able to use a fake context and use mock objects in my unit test. Of course I will use integration tests as well but they are not nearly as fast as some quick unit tests.

The error I receive when I try some implementations is that Error 2 'Project.Models.Order' does not contain a definition for 'State' and no extension method 'State' accepting a first argument of type '[whatever return type I use]' could be found (are you missing a using directive or an assembly reference?)

I hope someone can help me make a fake DbContext.Entry method.

like image 295
Erwin Rooijakkers Avatar asked Dec 09 '13 13:12

Erwin Rooijakkers


People also ask

How do you change the state of entity using DbContext?

This can be achieved in several ways: setting the EntityState for the entity explicitly; using the DbContext. Update method (which is new in EF Core); using the DbContext. Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.

Is DbContext a repository?

DbContext is already a repository, the repository is meant to be a low level abstraction. If you want to abstract different data sources create objects to represent those.

What is DbContext and DbSet in Entity Framework?

DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table.

What is DbContext entry?

DbEntityEntry Class DbEntityEntry studentEntry = dbcontext. Entry(entity); The DbEntityEntry enables you to access the entity state, and current and original values of all properties of a given entity. The following example code shows how to retrieve important information of a particular entity.


1 Answers

Found the answer here by "adding additional level of indirection" we get:

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

and use DbContext.SetModified(entity) in our controller. mockContext.Setup(c => c.SetModified(It.IsAny()));

like image 80
Erwin Rooijakkers Avatar answered Sep 19 '22 08:09

Erwin Rooijakkers