If you want to learn how to easily write unit tests for entity framework, read on. All your entity framework code will talk to a database, which under normal test conditions will make any unit test that calls entity framework code to fail. To make unit testing with Entity Framework possible, we want to hi-jack the entity framework context and point it to an in-memory store, rather than a database.
This might sounds complex, however, a great Nuget package exists called EFFORT (Entity Framework Fake ObjectContext Realization Tool). EFFORT allows you to mock entity framework so you can test your entity code as expected. In this guide, you will learn how to install and configure EFFORT. I will also include a unit test to show you how easy it is to write tests.
How To Install EFFORT
You can install EFFORT, via Nuget:
Make sure you use the correct version of EFFORT with the version of entity framework you are using. I'm assuming you'll be using entity framework 6, so use the EFFORT.EF6 package. If you use the wrong package, when you run your unit tests the code it will throw this error:
NotSupportedException Unable to determine the provider name for provider factory of type 'System.Data.EntityClient.EntityProviderFactory'.
Also remember, you only need to include the EFFORT package to your unit testing project. You do not need to add it to your production project. With EFFORT successful isntalled, it's time to configure it.
How To Configure EFFORT
First, I'm assuming you have a unit test project within your solution with EFFORT installed. In your test project you need to create this class:
Next, you need to configure your application to call this factory whenever a request to the Entity framework is made. This will route the request to the in-memory database instead. Within your app.config
in your unit testing project, you should have an entity framework section. In here you need to change the defaultconnectionfactory
setting. That's the configuration done with, let's write some tests!
The only change I made from the default setting was changing the type. With this format
➡ add your namespace.class name, Assembly Name
. Where namespace is your namespace, class
name is your class name, and assembly name is the name of your class library! Now we can start writing unit tests that will not fail!
Writing Tests With EFFORT
The code below shows a simple example:
Let's step through the code. On Line10, we define a method with the OneTimeSetUp
attribute. This registers the EFFORT
provider. Next, on Line16, we ensure our data store is empty, by using the ResetDb()
call. In our test, we can then create a new context instance and write the arrange, act, assert code. In this test, I add a single item into a collection. Line25, we need to save the result in memory. If you do not do this your context will be empty! Finally, on Line 29 we have everything configured and we can run our test. My code simply deletes everything from the store. Without using EFFORT this test would throw an exception. The code in case you care looks similar to this:
When I run this code, my test runs entity doesn't throw any exceptions, the entities get removed and I can write tests to prove it. Happy Coding 🤘