New to Telerik JustMock? Download free 30-day trial

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:

  1. Create a method that returns a fake collection of Dinners. 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; 
} 
  1. Create a new instance of the NerdDinners class.

  2. Arrange that a call to the nerdDinners.Dinners() method will return our fake collection.

  3. Call the nerdDinners.Dinners() and search for a dinner with a certain DinnerID in the Act.

  4. 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 the Telerik.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 the Telerik.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:

  1. Create an instance of the NerdDinners class.
  2. Create a new Dinner with some ID and a List of Dinner instances.
  3. Arrange nerdDinners.Dinners.Add() method to add the object from step 2. to the local collection from the same step.
  4. Arrange that the SaveChanges() method is doing nothing.
  5. Act by calling Add(dinner) and SaveChanges().
  6. Verify that:
    • the collection has exactly one item
    • this item is exactly the object from step 2.
In this article