3

I have an ASP.NET Core application (NetCoreApp1.1) Web API project and I would like to test a controller of that project. I added a .NET Core class library (targeting NetStandard1.6).

Now the problem I have is that according to Why doesn't Microsoft.NETCore.App support netstandard1.6? I can't reference the Web API project from that class library.

My question is then, does this mean that unless the controllers are placed somewhere else I won't be able to test them anymore? Maybe there is a way to do so but I haven't been able to achieve it in VS 2017 RC.

Community
  • 1
  • 1
Carlos Torrecillas
  • 4,965
  • 7
  • 38
  • 69

1 Answers1

3

Test projects should be console applications, not class libraries. A console application references Microsoft.NETCore.App and shouldn't have any problems referencing your Web API project.

A simple example of the project.json for a working test project is:

{
  "dependencies": {
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    },
    "xunit": "2.1.0",
    "MyApiProject": {
      "target": "project"
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dotnet"
    }
  },
  "testRunner": "xunit",
}

If you're using .csproj in VS 2017, it'll look different, but the principle should be the same. The test project can reference the API project locally, and uses a test runner like Xunit to run tests.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147