EF4.1 DbSet vs. EF4 ObjectContext and Unit Testing?

Fully simulating behavior of mocked layer is not point of unit testing. The point of unit testing is believing that mocked layer simply works. Unit test verifies the tested method not the mock.

You will just verify that correct method on the mock were called and perhaps you will set up some callbacks for modifying passed data if your business logic expects some values. Example: You have a business method inserting the record to the database and using the entity and its Id after insertion. The IObjectSet mock will be configured to: Set expectation that AddObject was called only once - you can set the expected instance in the verification You can define callback for AddObject to set Id to some value and use it later in the test DbSet will not make any difference - it is just wrapper around ObjectSet with similar behavior.In my opinion there is no efficient way to make mocks behave as real EF.

The effort needed for creating mock with behavior simulating EF + database will be much bigger then effort for your whole application! That will not be mock anymore it will be fake EF provider If you want to test your EF code (mapping, querying, persisting) and database behavior (like auto-increment, transactions, etc) you have to write integration tests. Here you have some related questions discussing repositories, unit of work and challenges with testing: ASP.

NET MVC3 and Entity Framework Code first architecture Organizationally, where should I put common queries when using Entity Framework Code First?

Fully simulating behavior of mocked layer is not point of unit testing. The point of unit testing is believing that mocked layer simply works. Unit test verifies the tested method not the mock.

You will just verify that correct method on the mock were called and perhaps you will set up some callbacks for modifying passed data if your business logic expects some values. Example: You have a business method inserting the record to the database and using the entity and its Id after insertion. The IObjectSet mock will be configured to: Set expectation that AddObject was called only once - you can set the expected instance in the verification You can define callback for AddObject to set Id to some value and use it later in the test DbSet will not make any difference - it is just wrapper around ObjectSet with similar behavior.In my opinion there is no efficient way to make mocks behave as real EF.

The effort needed for creating mock with behavior simulating EF + database will be much bigger then effort for your whole application! That will not be mock anymore it will be fake EF provider. If you want to test your EF code (mapping, querying, persisting) and database behavior (like auto-increment, transactions, etc) you have to write integration tests.

Here you have some related questions discussing repositories, unit of work and challenges with testing: ASP. NET MVC3 and Entity Framework Code first architecture Organizationally, where should I put common queries when using Entity Framework Code First?

Thanks for your response, that made a lot of sense. I'm fairly green to TDD, so some of these concepts are still sinking in. Your post helped a lot of things come together for me.

– Jerad Rose Jun 20 at 14:27.

Fully simulating behavior of mocked layer is not point of unit testing. The point of unit testing is believing that mocked layer simply works. Unit test verifies the tested method not the mock.

You will just verify that correct method on the mock were called and perhaps you will set up some callbacks for modifying passed data if your business logic expects some values. Example.

I'd like to use Mockito to write unit tests for this library. Is there a way I can go about this?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions