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

前端 未结 3 1171
既然无缘
既然无缘 2020-12-15 05:47

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 previe

相关标签:
3条回答
  • 2020-12-15 06:04

    With the release of RC2, the xUnit integration did not work any more with my projects (this has been fixed now see comments). To be able to test my projects I switched to NUnit. It appears to support RC2 and has a lightweight testrunner NUnitLite.

    Basically you need to host the NUnitLite into a console application and start it with "dotnet run".

    Add the following dependencies:

    "dependencies": {
        "NUnit": "3.2.1",
        "NUnitLite": "3.2.1",
    

    To start the test runner you need to add this code into the program.cs file:

    public class Program
    {
        public static void Main(string[] args)
        {
            new AutoRun().Execute(args);
        }
    }
    
    0 讨论(0)
  • 2020-12-15 06:12

    The xUnit team is doing a great job updating their documentation.

    In order to have information always updated, refer to the xUnit documentation at:

    Getting Started with xUnit.net

    0 讨论(0)
  • 2020-12-15 06:24

    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 superseded 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 menu Tools and then Extensions and Updates type in xUnit, and find the 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 file from Bitbucket.

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

    The Quick Summary:

    1. You have Visual Studio 2015 including Update 2 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 (an example is below).

    4. Write your class (example below).

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

    File 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.
    
    0 讨论(0)
提交回复
热议问题