In the previous blog post I showed how you can unit test with NBuilder and NSubstitute by using a FakeDbSet implementation. The thing is that we do not necessarily have to use a FakeDbSet but can also try and mock the DbSet. Let’s see how we can changes the implementation from last week’s blog post to mock DbSet instead. First thing to note is that a lot of examples for mocking the DbSet using other mocking frameworks such as Moq (such as this one) will demonstrate using DbSet and IQueryable, but it turns out that people using NSubstitute run into all sort of problems with this, as this SO question demonstrates.
NSubstitute is my current mocking framework of choice. It contains all the features I require in a mocking framework and is well maintained. I previously mentioned as one of my favourite Nuget packages for .NET and I also showed how you can mock abstract classes. In this blog post I want to talk a little bit about one of the many reasons I like NSubstitute, namely the ability to generate the output of a mocked function call based on the parameters passed into the function.