Mocking EF DataContext with Moq

So I wanted to mock the interface I use as base for my data context in my unit tests.
Mocking the interface itself is pretty easy, but I wanted to check if the properties of type DbSet have changed correctly.
So, all we need to do is mock the methods of the DbSet we are using, e.g. Add() and AddRange(), and use a callback to send the value of the method calls to a List.



As you can see, the method calls to a DbSet can easily be captured and send to another List which we can use for our asserts.

Comments

Popular posts from this blog

Debugging Lua scripts in VS Code using MoonSharp

Testing the response body of middleware in ASP.NET Core

Creating a basic MVC 6 web application with Entity Framework 7 and xUnit - Part 2