Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Shim DbContext with Microsoft Fakes?

I am creating a unit test that invokes a create task method. The create task method calls some mocked external object which are the purpose of my test, but inside my tested method there is also a SaveChanges call that saves my task. What I am trying to do is to shim the DbContext class inside System.Data.Entity so that the SaveChanges method no longer accesses the database.

I have added a fake assembly for System.Data.Entity but when I use System.Data.Entity.Fakes it says "cannot resolve symbol 'Fakes'", though I can access System.Data.Fakes (but I have't added a fake assembly for System.Data)

I have faked other methods in my entities context, but I can't fake SaveChanges method on my entity since the method is inherited from DbContext.

ShimDBEntities.AllInstances.SaveChanges = (x) => { ... };   

the upper chunk of code says "cannot resolve symbol 'SaveChanges'".

So how can I shim DbContext using Microsoft Fakes?

like image 880
Rzv.im Avatar asked Nov 03 '22 04:11

Rzv.im


1 Answers

I have managed to do it by creating a SaveChanges method in my BaseRepository class and use it in all of my derived repositories where I had db.SaveChanges.

Then I shimed my BaseRepository so that my SaveChanges method would do nothing

like image 100
Rzv.im Avatar answered Nov 14 '22 17:11

Rzv.im