.NET Core

Setting up Moq with your .NET Core test project

Keen to use Moq in your .NET Core project (i.e. ASP.NET Core and XUnit)? Here’s how to get setup.

Background

Moq is a mocking framework for .NET. If you’d like to learn more about Moq, check out their GitHub page.

Adding Moq in your Test Project

I’m assuming you have a test project using your favourite unit test framework such as XUnit, NUnit, MSTest, etc. I’m using XUnit.

Step 1: Add the Moq NuGet package

As of this writing, only the pre-release version supports .NET Core and the current version is 4.6.38-alpha

Install-Package Moq -prerelease

Step 2: Add TraceSource package

This is an important step – one that caused me a lot of pain. It’s required but not set as a dependency. Without it you will get IO File exceptions when you try to use a mocked object.

Install-Package System.Diagnostics.TraceSource -version 4.0.0

Complete project.json File

The following is a complete example of the project.json.

Note that this project.json belongs to a project running .NET Core 1.1, hence the netcoreapp1.1 entry.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s