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.

2
  • Why don't you create an xunittest project to run your tests? Commented Jan 18, 2017 at 18:25
  • Would that allow referencing a NetCoreApp? Commented Jan 18, 2017 at 18:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked and made sense. Thanks! My next challenge is to get NUnit running from VS 2017 RC

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.