Unit Test for EF LINQ queries using Mocked DbSet

Goal: 

Create a unit test for a EF repository query. As we all know unit tests are infrastructure agnostic and therefore have no kowledge of for example external services, databases, etc. So how can we create a unit test for a repository query without hitting the DB? Mocked DbSet to the rescue:

Solution:

Create an extension method on collections that can be used to mock the data/collections returned by EF instead of hitting the DB.

//A List of allocations for a gas system that we want to return when the EF LINQ query is executed

var allocationVOMockDbSet = new List

{

new AllocationVO

{

LocationId = -1,

ContractId = -1,

NominationId = null,

AllocatedQuantity = 1000,

AdjustmentIndicatorType = AdjustmentIndicatorType.Measurement,

GasDayId = -100,

GasDay = new GasDay(null, DateTime.Today),

AccountingPeriod = new YearMonth(2016, 10)

},

//new

new AllocationVO

{

LocationId = -1,

ContractId = -1,

NominationId = null,

AllocatedQuantity = 1500,

AdjustmentIndicatorType = AdjustmentIndicatorType.Other,

GasDayId = -100,

GasDay = new GasDay(null, DateTime.Today),

AccountingPeriod = new YearMonth(2016, 10)

},

}

.AsMockDbSet();  //here we define the list as a mocked set

//Now we use the StructureMap container to create an inject the DbContext of IAfterFlowContext

var afterFlowContextMock = thisContainer.InstantiateAndInjectMock();

//Now we add the setup for the mocked context on Set of AllocationVO (a domain entity) that returns the mocked set

afterFlowContextMock.Setup(s => s.Set()).Returns(allocationVOMockDbSet.Object);

//Here is the extension for collections on DbSet that makes it possible to mock all the different implementations of the DbSet

public static Mock<DbSet> AsMockDbSet(this ICollection source) where T : class

{

var queryable = source.AsQueryable();

var dbSet = new Mock<DbSet>();

dbSet.As<IQueryable>().Setup(m => m.Provider).Returns(queryable.Provider);

dbSet.As<IQueryable>().Setup(m => m.Expression).Returns(queryable.Expression);

dbSet.As<IQueryable>().Setup(m => m.ElementType).Returns(queryable.ElementType);

dbSet.As<IQueryable>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator);

dbSet.Setup(s => s.Add(It.IsAny())).Callback(source.Add);

dbSet.Setup(s => s.Include(It.IsAny())).Returns(dbSet.Object);

dbSet.Setup(x => x.AsNoTracking()).Returns(dbSet.Object);

return dbSet;

}

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.