With Microsoft Entity 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.
In this topic we will cover some scenarios in unit testing Microsoft Entity Framework.
In the examples below the DbContext class is used, along with the following methods:
| C# | Copy |
|---|
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 will need a method that returns a fake collection of Dinners. We will use the example below:
| C# | Copy |
|---|
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;. |
| C# | Copy |
|---|
[TestMethod]
public void ShouldReturnFakeCollectionWhenExpected()
{
NerdDinners nerdDinners = new NerdDinners();
Mock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners());
var query = from d in nerdDinners.Dinners
where d.DinnerID == 1
select d;
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.
| C# | Copy |
|---|
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 durring the Act, we will make a repository class:
| C# | Copy |
|---|
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 the Future Mocking.
Important |
|---|
Note that when you use ReturnsCollection() you must be using the Telerik.JustMock.Helpers;. |
| C# | Copy |
|---|
[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.
| C# | Copy |
|---|
[TestMethod]
public void ShouldFakeAddingNewEntityToContext()
{
NerdDinners nerdDinners = new NerdDinners();
var dinner = new Dinner { DinnerID = 1 };
IList<Dinner> dinners = new List<Dinner>();
Mock.Arrange(() => nerdDinners.Dinners.Add(dinner)).DoInstead((Dinner d) => dinners.Add(d));
Mock.Arrange(() => nerdDinners.SaveChanges()).DoNothing();
nerdDinners.Dinners.Add(dinner);
nerdDinners.SaveChanges();
Assert.AreEqual(1, dinners.Count);
Assert.AreEqual(1, dinners[0].DinnerID);
}
|
This is what we do here:
- Create an instance of the NerdDinners class.
- Create a new Dinner with some ID and a List of Dinners
- 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) and SaveChanges().
- Verify that:
- the collection has exactly one item
- this item is exactly the object from step 2.
See Also