views:

3480

answers:

10

I want to test my Entities that are built using Entity Framework. My concern is that using Entity Framework means directly working with data source. So any ideas how to unit testing Entity Framework based components?

+4  A: 

Apparently it's very hard. Eloquently put by Erik here - http://stackoverflow.com/questions/316897/tdd-and-adonet-entity-framework#answer-316972

gef
A: 

How about using a mocking framework? It seems to me that a mocking framework can help you isolaye your business logic from the database.

Dror Helper
+1  A: 

I agree, a mocking framework is what you're after. You create "mocked" objects that aren't retrieved from your datasource, and you test the data in that object. I personally have been working with Moq, and I like it--there is also Rhinomocks, plus others.

Carl
+5  A: 

You are going to want to use a Mocking Framework to retrieve mock values rather than hitting the real data. Here are a list of a few mocking frameworks and links to some screencasts to help you get started:

Here are some screencasts on how to get started:

David P
You can't do this easily with the EF. Your answer is correct for interfaces or extendable POCOs.
Will
+1  A: 

I wonder how Mock would serve instead of database!! But I would give it a try thank you all.

mosessaur
+2  A: 

Although the examples might be very simplistic I have attempted to discuss a possible solution to this very issue. It involves separation of concerns and our dear friend Dependency Injection.

http://devblog.petrellyn.com/

Contact me if you want more details.

Jamie Phillips
+2  A: 

I would like to share another input to this. I was able to test Entity Framework based components and application using TypeMock Isolator as well. However it is commercial.

Have a look at this post: Introducing Entity Framework Unit Testing with TypeMock Isolator

mosessaur
+3  A: 

Due to the fact that version 1 of the Entity Framework breaks a few major software design principles, there really isn't any way to apply TDD when using it in your application. My research points to NHibernate if you're looking for an immediate solution. It was designed with unit testing in mind.

However, if you can wait, there appears to be hope for the next release of the Entity Framework: Test-Driven Development Walkthrough with the Entity Framework 4.0

Jason Stonebraker
+5  A: 

For Enity Framework 4, this looks promising: Testability and Entity Framework 4.0

Hans Kesting
A: 

The BookLibrary sample application of the WPF Application Framework (WAF) project shows how an Entity Framework based application can be unit tested.

jbe