Entity Framework Core 2 Unit Testing in .NET Core 2

Full source code available here.

Unit testing Entity Framework used to be quite a chore, but over the past few years it has become significantly easier.

In this post I’m going to show you how to use the InMemory database with named instances.

The ProductsController is fairly standard, it takes a DbContext in the constructor which is instantiated by dependency injection (see the Startup.cs in the source code if you don’t know how to do this).

1public ProductsController(ProductsContext context)
3    _productsContext = context;

The action methods use the context to query the database and return responses to the callers. From the response status code the caller knows if there is also content (products) in the response.

 2public async Task<IActionResult> Get()
 4    var products = await _productsContext.Products.ToListAsync();
 6    if (!products.Any())
 7    {
 8        return NotFound();
 9    }
10    return Ok(products);


In the unit test project, (see here if you need help creating this), I’ve added a ProductsControllerTests class.

In my Get test I created an instance of a DbContextOptions by using the DbContextOptionsBuilder. On the builder I specify that I want an InMemoryDatabase and give it a unique name for this test - this is important, if the name is not unique across all tests or if it is left blank you will end up with a database shared by more than one test.

In the Arrange portion of the test I also instantiate the ProductsContext; then instantiate a few products, add them to the context; don’t forget to save the changes; finally create an instance of the of ProductsController.

Here is the full Arrange portion of the test.

 1// Arrange
 2var options = new DbContextOptionsBuilder<ProductsContext>()
 3.UseInMemoryDatabase(databaseName: "Get test")
 6ProductsContext productsContext = new ProductsContext(options);
 7Product product01 = new Product { Name = "first", ProductId = 1, Sku = "abc" };
 8Product product02 = new Product { Name = "second", ProductId = 2, Sku = "def" };
12await productsContext.SaveChangesAsync();
13ProductsController controller = new ProductsController(productsContext);

Next I Act, just calling the controller’s Get method. The controller already has the context and the get method will query it just like a real database.

1// Act
2var actionResult = await controller.Get();

Then I Assert.

1// Assert
2var okObjectResult = actionResult as OkObjectResult;
4List<Product> products = okObjectResult.Value as List<Product>;
6Assert.Equal(2, products.Count());

Note the as OkObjectResult type, I know it’s that type because that is what I return from the ProductsController.

I assign the value of the okObjectResult to a List.Assert that there are two entries in the list.

Testing NotFound - 404

In the ProductsController I also have a Get that takes a productId as an int and returns a product. You should also test the scenario that no entries in the database match the search. In this case I am returning a NotFound, you may choose to return something else like an OK with nothing in it. In the case of a list, some people like to return an empty list; there is much debate on this topic and I have no interest in adding my two cents.

To test the NotFound response, the Act and Assert change a little. I call the GET method with an id that I know will return nothing.

1// Act
2var actionResult = await controller.Get(999);
4// Assert
5var notFoundResult = actionResult as NotFoundResult;
7Assert.Equal(404, notFoundResult.StatusCode);

Full source code available here.

comments powered by Disqus