Entity Framework Mocking

With Microsof tEntity Framework you develop data access application by using a conceptual application model instead of relational storage schema.

Telerik® JustMock can be used in conjunction with Microsoft Entity Framework, with the Telerik.JustMock.EntityFramework package. With it, you can easily create in-memory mocks of the DbSet and DbContext types. It also provides some additional mocking amenities for JustMock.

In this topic we will cover some scenarios in unit testing Microsoft EntityF ramework. In the examples below the DbContext class is used, 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

First we need a method that returns a fake collection of Dinners. We will use the example 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; 
} 
After creating a new instance of the NerdDinners class, we will Arrange that a call to nerdDinners.Dinners() method, will return our fake collection. Then, in the Act, we call the nerdDinners.Dinners() and search for a dinner with a certain DinnerID. Finally we 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
Not finding the help you need? Improve this article