Functional Testing of ASP.NET core 2.1 MVC Application

Ratan Parai
HackerNoon.com
2 min readMay 23, 2018

--

In ASP.NET core 2.1, setting up functional testing project got much easier with the release of Microsoft.AspNetCore.Mvc.Testing nuget package. In this post we are going to setup a functional test project.

Prerequisite

To follow this tutorial you should have-

  1. .NET Core 2.1 RC1 SDK and
  2. VS Code or Microsoft Visual Studio 2017 v15.7 Preview 1 or newer, installed on your system

Create test project

Create a folder and name it HelloWorld, because why not 😜. Open PowerShell window inside the folder (Shift + right clickanywhere inside the folder and select Open PowerShell window here) and create a solution:

Now create a basic MVC project inside src directory and xunitproject inside tests directory:

Add those two project to the solution:

Reference the MVC project form the FunctionalTests project:

Write functional Test

Add Microsoft.AspNetCore.Mvc.Testing to the functional test project:

Now open the project in VS Code or Visual Studio 2017 15.7 Preview 1 or newer and create a new class inside HelloWorld.FunctionalTests project and name it HomePageShould.cs

Run the test

Now run the test. It should fail with the error message

Message: System.IO.FileNotFoundException : Could not load file or assembly ‘Microsoft.AspNetCore, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’. The system cannot find the file specified.

To resolve it, add Microsoft.AspNetCore.App nuget package to the test project

Now run the test from Test>Run>All Tests (Visual Studio 2017) or from PowerShell-

You should see the green tick of happiness.

--

--