How to test a controller in .NET Core

Carlos Torrecillas

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.

Nate Barbettini

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Test a controller in .Net Core always returns false?

How to unit test MVC controller that uses HttpContext in ASP.NET Core 1.1

Unit test .NET Core 3 Web API Controller

Adding claims to httpcontext in asp.net core (Controller test)

Asp.net Core Identity unit test controller actions

How are controller actions invoked Asp .Net Core?

NET Core 5 How to override an action on a controller

.Net 6 core: How to inject a configuration to the controller?

How to unit test ADF PipeLineRun in .Net Core?

.NET Core how to unit test service?

Net Core custom controller

how to test "save" method of a service using Nunit test in .net core

.NET Core with NUnit test

How to navigate to an ASP.NET Core MVC controller in Blazor app?

How to retrieve the controller type in ASP.NET Core?

How to pass two parameters to a controller in ASP.NET Core MVC?

How to change the default controller and action in ASP.NET Core API?

How to add action to controller in api mvc project net core 3.1

How to use generic methods in Controller with ASP.NET Core

How to draw picture in asp.net core controller

How to do Caching and Dependency Injection outside of Controller in .NET Core MVC

How to add extension method in .Net core API controller

How to use a controller in another assembly in ASP.NET Core 3.0?

How to tell ASP.NET Core view exists from controller?

How to access field result from returned controller in MVC .net core

How to receive `multipart/mixed` in an ASP.NET Core controller

How to render action of specific controller in master Layout view in .NET Core?

How to access controller from partial views in .net core?

asp.net core swashbuckle how to use versioning in same controller

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive