1

From what I understand, Mocking is used to remove dependency of another service so that you can properly test the execution of business logic without having to worry about those other services working or not.

However, if what you are testing IS that particular service (i.e. Entity Framework), Implementation-style unit tests against a preset test database are really the only tests that will tell you anything useful.

Am I missing anything? Does mocking have any place in my testing of an Entity Framework DAL?

4 Answers 4

1

You are correct in your assertion about mocking:

Mocking is used to remove dependency of another service so that you can properly test the execution of business logic without having to worry about those other services working or not.

In my words: the idea behind unit testing is to test a single code path through a single method. When that method hands execution over to another object there is a dependency. When control passes to an unmocked dependency you are no longer unit testing, but are instead integration testing.

Data access layer testing is typically an integration test. As you speculate you can utilize a predictable data set to ensure your DAL is returning results as expected. I would not expect a DAL to have any dependencies which would require mocking. Your testing that the values returned by your DAL match what you would expect given your dataset.

All of the above said it is not your responsibility to test the Entity Framework. If you find yourself testing the way EF works and are not creating tests about your specific DAL implementation then you are writing the wrong tests. Put another way: you test your code, let someone else test theirs.

Finally, three years ago I asked a similar question which elicited answers which greatly improved my understanding of testing. While not identical to your question I'd recommend reading through the responses.

Sign up to request clarification or add additional context in comments.

Comments

1

In my opinion, mocking objects has nothing to do with the layer you are about to test. You have a component that you want to test and it has dependencies (that you can mock). So go ahead and mock.

Comments

1

One can assume EF works. You would test your code that interacts with EF. In this case, you fake EF in order to test your interacting code.

You basically shouldn't be testing EF. Leave that to Microsoft.

2 Comments

It may sound silly, but this is actually the answer that I needed to hear. I mean, I had Unit Tests that essentially just make sure that DbContext.Add() actually adds something to the database. I just need to catch myself when I find myself testing code that isn't mine =)
It's all part of the unit testing learning journey. :) I recommend this book. artofunittesting.com
0

One thing you might consider is that EF can work with a local file and not your actual repository and you can switch between the two with connection strings. This example would create the .sdf file in an AppData folder or in the bin folder if it's a console application.

<connectionStrings>
<add name="SecurityContext" 
     connectionString="Data Source=|DataDirectory|YourDBContext.sdf"
     providerName="System.Data.SqlServerCe.4.0" />

I like this when I'm starting a project or testing. You can load the DB with data and such and presto: EF has a mocked DB for you to run tests against without touching production data.

1 Comment

Do you usually have a persistent file that you load with data once and somehow revert to its original state after testing, or do you have your Integration Test set it up, test it, and destroy it every time?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.