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:
public class NerdDinners : DbContext
{
public DbSet<Dinner> Dinners { get; set; }
public DbSet<RSVP> RSVPs { get; set; }
}
public class Dinner
{
public int DinnerID { get; set; }
public string Title { get; set; }
public DateTime EventDate { get; set; }
public string Address { get; set; }
public string HostedBy { get; set; }
}
public class RSVP
{
public int RSVPID { get; set; }
public int DinnerID { get; set; }
public string AtendeeEmail { get; set; }
}
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:
public IList<Dinner> FakeDinners()
{
List<Dinner> fakeDin = new List<Dinner>
{
new Dinner { Address = "1 Microsoft way", DinnerID = 1, EventDate =DateTime.Now, HostedBy = "Telerik" , Title = "Telerik Dinner"}
};
return fakeDin;
}
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;
.
[TestMethod]
public void ShouldReturnFakeCollectionWhenExpected()
{
NerdDinners nerdDinners = new NerdDinners();
// Arrange
Mock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners());
// Act
var query = from d in nerdDinners.Dinners
where d.DinnerID == 1
select d;
// Assert
Assert.AreEqual(1, query.Count());
Assert.AreEqual(1, query.First().DinnerID);
}
Returning A Fake Collection with Future Mocking
In this example we will return the same fake collection.
public IList<Dinner> FakeDinners()
{
List<Dinner> fakeDin = new List<Dinner>
{
new Dinner { Address = "1 Microsoft way", DinnerID = 1, EventDate =DateTime.Now, HostedBy = "Telerik" , Title = "Telerik Dinner"}
};
return fakeDin;
}
To assure that the instance does not matter during the Act phase we will make a repository class:
public class DinnerRepository
{
public Dinner GetById(int dinnerId)
{
NerdDinners nerdDinners = new NerdDinners();
var query = from d in nerdDinners.Dinners
where d.DinnerID == 1
select d;
return query.First();
}
}
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.
[TestMethod]
public void ShouldReturnFakeCollectionForFutureInstance()
{
NerdDinners nerdDinners = new NerdDinners();
Mock.Arrange(() => nerdDinners.Dinners).IgnoreInstance().ReturnsCollection(FakeDinners());
Assert.AreEqual(1, new DinnerRepository().GetById(1).DinnerID);
}
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.
[TestMethod]
public void ShouldReturnFakeCollectionForFutureInstance()
{
NerdDinners nerdDinners = new NerdDinners();
Mock.Arrange(() => nerdDinners.Dinners).IgnoreInstance().ReturnsCollection(FakeDinners());
Assert.AreEqual(1, new DinnerRepository().GetById(1).DinnerID);
}
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.