Warren  P Warren P - 2 months ago 26
C# Question

How do you unit test an ASP.NET Core controller or model object?

I am trying to get some controller, model, and repository (data access) C# classes under unit test, in Visual Studio 2015, with ASP.NET Core MVC (ASP.NET 5 during the preview, now called ASP.Net Core) apps.

I have the following structure:

Solution
|
src
|
|-- ITConsole <- main app (ASP.NET MVC, DNX 4.5.1)
|
`-- ITConsoleTests <- What kind of project should this be?


The MainApp is using DNX 4.5.1, but it seems that if I create a standard nUnit Unit test app, it's only available as a classic .NET Framework class library, targetting .NET Framework 4.5.2, not as a Web Class Library that can work with my main app.

So, just in case it might work as a classic .NET framework Microsoft Unit Test framework project (.net assembly), I tried to manually find and add references (by add reference, and browse) to get the .NET dependencies to resolve. I am aware that .NET assembly references are sadly non-transitive. So if UnitTest.dll has a reference to MainApp.dll, and MainApp.dll depends on ASP.NET MVC, and everything else that it depends on, I have to do that myself. That is what I'm trying to do. I added a reference to
C:\dev\Demo\ITConsole\artifacts\bin\ITConsole\Debug\dnx451\ITConsole.dll
into my unit test project so I could begin to get the code to compile. The unit test classes compile but they don't run, probably because of the mess of trying to add a reference to ASP.NET.

Right now, even though I have added a reference to Common.Logging.Core, and Common.Logging, when I click "Run All" on the Test explorer I get this error:

Test Name: TestStudyLogReadDocument
Test FullName: ITConsoleTests.ITConsoleTestStudyLog.TestStudyLogReadDocument
Test Source: C:\dev\Demo\ITConsole\ITConsoleTests\ITConsoleTestStudyLog.cs : line 52
Test Outcome: Failed
Test Duration: 0:00:00.0712058

Result StackTrace:
at Couchbase.Configuration.Client.ClientConfiguration..ctor()
at ITConsole.Repository.StudyLogRepository..ctor() in C:\dev\Demo\ITConsole\src\ITConsole\Repository\StudyLogRepository.cs:line 39
at ITConsoleTests.ITConsoleTestStudyLog.SetupDb() in C:\dev\Demo\ITConsole\ITConsoleTests\ITConsoleTestStudyLog.cs:line 30
at ITConsoleTests.ITConsoleTestStudyLog.TestStudyLogReadDocument() in C:\dev\Demo\ITConsole\ITConsoleTests\ITConsoleTestStudyLog.cs:line 53
Result Message:
Test method ITConsoleTests.ITConsoleTestStudyLog.TestStudyLogReadDocument threw exception:
System.IO.FileLoadException: Could not load file or assembly 'Common.Logging.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)


(At the time this question was asked...) None of the asp.net 5 mvc preview templates can generate unit tests for you. Can you even unit test a shiny new ASP.NET Core application? See screenshot below, for example of how the normal way you get started unit testing is not available in VS 2015 using MSTEST.

no unit test for you

Answer

UPDATE: XUnit is still a great idea, but this answer is outdated now because you can also use the "standard" MSTEST if you want with ASP.NET core. (June 1, 2016) I find I still prefer XUnit, but it's your call.

MOST RECENT XUNIT INSTRUCTIONS LINK: Excellent instructions that may be updated more often than this answer are found at the xUnit wiki.

IDE WORKAROUND: manually find and delete %TEMP%\VisualStudioTestExplorerExtensions when Visual Studio goes stupid and won't "detect" and show you your tests.

As of May 2016, with ASP.NET Core 1.0 RC1 recently superceded by RC2, it still does not appear possible to use the standard Microsoft Unit Test framework with ASP.NET Core (Formerly ASP.NET 5), and XUnit appears to be a good choice for RC1 and RC2.

You can get XUnit.net unit testing to work with ASP.NET Core 1.0.0-RC1, using the official instructions]2 at the XUnit github project which has a specific ".net core getting started" case.

You can also install the XUnit New Project Template that provides a templated unit test project for regular full .net and .net core. Click Tools and then Extensions and Updates type in XUnit, and find xUnit Test Project TEMPLATE and install the TEMPLATE. DO NOT INSTALL ANY xUNIT TEST RUNNER, YOU DO NOT NEED IT.

I have created a working sample and uploaded it to bitbucket:

https://bitbucket.org/wpostma/aspnet5mvc6xunitdemo

If you don't have mercurial, you can download a zip from bitbucket.

The demo includes one test that passes, and one test that fails.

The Quick Summary:

  1. You have Visual Studio 2015 including Update2 and the "1.0.0 preview" tools (latest as of May 2016).

  2. Create a Web Class Library NOT a Unit Test Project.

  3. Add XUnit references to it, and fix your project.json (example below).

  4. Write your class (example below).

  5. Run tests with Test Explorer inside ide, or outside ide, type in dnx . tests and examine output (example below).

project.json for 1.0.0-rc2 with reference to a demo assembly and xunit:

 {
  "version": "1.0.0-*",

  "testRunner": "xunit",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },

    "dotnet-test-xunit": "1.0.0-rc2-*",

    "xunit": "2.1.0",


    "YetAnotherWebbyDemo": "1.0.0-*"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  }
}

Unit test class (whatever.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Xunit;

using YetAnotherWebbyDemo.Models;

namespace YetAnotherWebbyDemoTests
{
    // This project can output the Class library as a NuGet Package.
    // To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
    public class TestBasics
    {
        [Fact]
        public void TestAdd()
        {

            TestableModelClass TestMe = new TestableModelClass();


            Assert.True(TestMe.Add(3, 2) == 5, "Basic Math Failure");

            Assert.True(TestMe.Add(-3, -2) == -5, "Basic Math Failure");
        }

    }
}

Example output from commandline in RC1 when we used dnx:

C:\dev\Demo\YetAnotherWebbyDemo\src\YetAnotherWebbyDemoTests>dnx . test

xUnit.net DNX Runner (32-bit DNX 4.5.1)
  Discovering: YetAnotherWebbyDemoTests
  Discovered:  YetAnotherWebbyDemoTests
  Starting:    YetAnotherWebbyDemoTests
    YetAnotherWebbyDemoTests.TestBasics.TestAdd [FAIL]
      Basic Math Failure
      Expected: True
      Actual:   False
      Stack Trace:
        YetAnotherWebbyDemoTestBasics.cs(25,0): at YetAnotherWebbyDemoTests.Test
Basics.TestAdd()
  Finished:    YetAnotherWebbyDemoTests
=== TEST EXECUTION SUMMARY ===
   YetAnotherWebbyDemoTests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 0.263s

Example output in RC2 where we're using dotnet:

D:\dev\aspnet5mvc6xunitdemo\src\YetAnotherWebbyDemoTests>dotnet test
Project YetAnotherWebbyDemo (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Project YetAnotherWebbyDemoTests (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
xUnit.net .NET CLI test runner (64-bit win10-x64)
  Discovering: YetAnotherWebbyDemoTests
  Discovered:  YetAnotherWebbyDemoTests
  Starting:    YetAnotherWebbyDemoTests
    YetAnotherWebbyDemoTests.TestBasics.TestAdd [FAIL]
      Basic Math Failure
      Expected: True
      Actual:   False
      Stack Trace:
        D:\dev\aspnet5mvc6xunitdemo\src\YetAnotherWebbyDemoTests\YetAnotherWebbyDemoTestBasics.cs(26,0): at YetAnotherWebbyDemoTests.TestBasics.TestAdd()
  Finished:    YetAnotherWebbyDemoTests
=== TEST EXECUTION SUMMARY ===
   YetAnotherWebbyDemoTests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 0.205s
SUMMARY: Total: 1 targets, Passed: 0, Failed: 1.