hannes neukermans
hannes neukermans

Reputation: 13257

How to unit test Asp.net core WebAPI (net452) project?

I currently have a Asp.net Core Web.Api project.
It (only) targets the net452 framework.
Question is: How do I unit test an asp,net core WebAPI (net452) project?

I already tried adding a classic .net unit testing project to my solution, with a reference to my web.api core project. For some reason, I'm not able to declare (refer) any class inside my webapi project. From this, I conclude, I cannot use a classic unit testing project to test my Asp.net core project.

inside my project.json:

 "frameworks": {
   "net452": {}
   }
}

this is how my solution looks like:

solution

inside my test class:

[TestClass]
public class ActiveDirectoryClientProviderTests
{
    [TestMethod]
    public async Task Get_Should_Return_Client()
    {
        //var settings = new ActiveDirectorySettings();
        Ng2Aa_demo.Domain.Avatar.GetAvatarByUserHandler
    }
}

Upvotes: 4

Views: 3386

Answers (2)

hannes neukermans
hannes neukermans

Reputation: 13257

  1. First you need to add another core project enter image description here

  2. Inside your project.json add 2 dependencies & specify the testRunner. Make sure to use Xunit !!

enter image description here

"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029"

Now you can unit test your web api core project with xunit !

Upvotes: 0

thepirat000
thepirat000

Reputation: 13089

You can use xUnit or NUnit, both works well with .Net core.

Here is a tutorial on how to create the unit test project: https://learn.microsoft.com/en-us/dotnet/articles/core/testing/unit-testing-with-dotnet-test

You basically create a .Net core class library, import the xUnit or NUnit library and set the unit test runner.

An example of a project json using xUnit:

{
  "version": "1.0.0-*",
  "testRunner": "xunit",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type":"platform",
      "version": "1.0.0"
    },
    "xunit":"2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-192208-24",
    "PrimeService": {
      "target": "project"
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": [
        "dotnet5.4",
        "portable-net451+win8"
      ]
    }
  }
}

Taken from here

Upvotes: 3

Related Questions