Entity Framework Mocking
Telerik® JustMock allows you to perform unit testing in conjunctions with the Microsoft Entity Framework.
Introduction
With Microsoft Entity Framework, you develop data access applications by using a conceptual application model instead of a relational storage schema.
JustMock supports both EntityFramework and EntityFrameworkCore thanks to the extension packages respectively Telerik.JustMock.EntityFramework and Telerik.JustMock.EntityFrameworkCore. The packages allows you to easily create in-memory mocks of the DbSet and DbContext types. It also provides additional mocking amenities for JustMock.
In this topic, we will cover some common scenarios in the EntityFramework unit testing. In the examples below, we use the DbContext
class along with the following methods:
Returning A Fake Collection
The following steps demonstrate how to return a fake collection:
- Create a method that returns a fake collection of
Dinner
s. For this example, we use the code below:
Create a new instance of the
NerdDinners
class.Arrange that a call to the
nerdDinners.Dinners()
method will return our fake collection.Call the
nerdDinners.Dinners()
and search for adinner
with a certainDinnerID
in the Act.Assert that there is only one item in our collection and this item has
DinnerID
equal to one.
Important
Note that when you use
ReturnsCollection()
you must be using theTelerik.JustMock.Helpers;
.
Returning A Fake Collection with Future Mocking
In this example we will return the same fake collection.
To assure that the instance does not matter during the Act phase we will make a repository class:
As you see, in the test below we are acting with a new DinnerRepository()
, but still we are meeting the expectations and the test passes. This behavior is known and expected in Future Mocking.
Note that when you use
ReturnsCollection()
you must be using theTelerik.JustMock.Helpers;
namespace.
Faking the Add of an Entity
In the next example we will Arrange the calling of the Add()
method to actually add an item to a previously created local collection.
Here are the steps:
- Create an instance of the
NerdDinners
class. - Create a new
Dinner
with some ID and aList
ofDinner
instances. - Arrange
nerdDinners.Dinners.Add()
method to add the object from step 2. to the local collection from the same step. - Arrange that the
SaveChanges()
method is doing nothing. - Act by calling
Add(dinner)
andSaveChanges()
. - Verify that:
- the collection has exactly one item
- this item is exactly the object from step 2.