1

I have read a lot about unit testing the Entity Framework.

I am posting this question because I simply saw there are too many solutions to this problem !

Here are the solutions I found:

  1. Use an expensive commercial tool called TypeMock (mentioned here).
  2. Use an alpha open-source tool called Effort (mentioned here).
  3. Use Repository Pattern and Rhino Mock. test the isolated LINQ queries against a real database (mentioned here).

Some problems with some of the methods stated here:

  • You cannot get around the fact that you need to supply an ObjectContext with a connection string
  • If you fake the ObjectContext - some things that might work in unit testing won't work in production (like running functions inside the queries)

Some of the articles I read were from 3-4 years ago.

Does any one here have any experience with this issue and can help me go for the best solution ?

Just to make things clear: my business logic functions aren't just simple functions like 'GetUserById'.

Some of the functions include accessing objects, that have relationships to other objects. (for example - I can add a user + departmant + office in the same function).

Community
  • 1
  • 1
John Miner
  • 893
  • 1
  • 15
  • 32

1 Answers1

2

For doing stuff like this I would recommend using the Repository pattern and use a mocking framework like Rhino or MOQ to test your business logic and I would then recommend you do some integration tests for your repository.

First this follows the "Single Responsibility Principal", and allows you to test your business logic with out nearly as much overhead (Mocking ObjectContext is a pain) and it allows you to test your queries with real data. I would strongly state any well tested solution is going to include both Unit and Integration testing.

Bob The Janitor
  • 20,292
  • 10
  • 49
  • 72
  • I have never used Rhino or MOQ. Do you have any samples that can help me out ? I don't understand how I should to separate the LINQ code out from the business logic. Where do I put the 'entities.SaveChanges' ? in the business logic ? or in the part that is tested by Rhino\MOQ ? what if my function in the BL has 2 calls to the database - one to get a list of items and one to iterate over each item and perform some action ? – John Miner Jun 06 '12 at 17:31
  • and another question - does this method you propose support lazy loading ? I don't want for each database call to do '.ToList()', cause it might bring huge amounts of data from the database... – John Miner Jun 06 '12 at 17:32
  • you don't have to remove the linq, I would look at using a Unit Of Work pattern here is an Example : http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/, it's a little older but concepts are still the same, you put your repositories in a DAL and then call them though the Unit Of Work – Bob The Janitor Jun 06 '12 at 20:10
  • I don't understand how the repository helps me out with the testing. And also - the repository in the link you gave has a '.ToList()' operation in the 'GetAll' function. This is problematic, because it could shift loads of information from the database to my app unnecessarily ! – John Miner Jun 06 '12 at 21:44