January 2018 Entries

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 ......