Skip to content

Testing

How a Little Environment Variable Can Break Your Integration Tests in ReSharper & Rider

Last week we run into a strange problem with our integration tests. On some developer machines everything worked fine, while other machines got errors like this one:

System.InvalidOperationException : Cannot resolve scoped service 'my.application.IOrderRepository' from root provider. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)

Serve Test Data From ASP.NET Web API in .Net 8

If you need an API endpoint that gives you some (static) test data, you can join the search for the most complicated solution. My current favourite in unnecessary complexity is to use MongoDB to serve a handful of JSON objects.

However, if you prefer a much lighter approach and already use .Net, I strongly recommend that you try Web API for this task. It only takes a few lines to get your data in a format that you can access from your application. And if you want to keep going on, you can add more functionality with ease.

Where to Find Test Files?

If we want to test our code, we often need files with the right format. We can create a text file without much effort, but what do we do if we need a specific video format or an audio file in OGG?

Luckily, we are not the only ones with this problem. There are dedicated sites that offers a wide range of file formats that we can use for testing.

How to Migrate from Moq to NSubstitute

I was a happy user of the Moq library for the last 12 years. I liked the simplicity of Moq and how it allowed me to write tests first and then incrementally build the functionality.

Unfortunately, the creator of Moq is currently doing his best to kill the project. As he declared, either his spyware SponsorLink strong-arms enough individual developers into sponsoring him, or he will quit developing Moq. This leaves us in a lose-lose situation and for me it is time to move my private projects to another mocking library.

I played with a few alternatives and finally settled with NSubstitute. It covers all my use cases and offers an even simpler syntax than what I know from Moq.

Great Help & Little Effort: Project Templates for Visual Studio

If I run into tasks that I must repeat countless times, I try to automate them as much as possible. Since we always add the same dependencies to our test projects, we thought it would be a great idea to create a project template that has all the necessary settings already in place. Let us figure out how little effort it takes to create our own project templates for dotnet, Visual Studio and Rider.

Code Coverage made Easy: A Step-by-Step Guide for .NET 6

For .Net 4.8 it was enough to install Visual Studio Enterprise Edition on the build server and check the box for code coverage in the Testing task of Azure DevOps. The build agent then collected the coverage data and published it as part of your builds. Unfortunately, for .Net 6 this no longer works, and we need to go through a lot more steps.

There are a handful projects for code coverage that support .Net "core", but not every project is still under active development. Be careful when you select a coverage tool and make sure that there is still some activity going on.