gpt4 book ai didi

c# - 如何对 ASP.NET Core Controller 或模型对象进行单元测试?

转载 作者:IT王子 更新时间:2023-10-29 04:39:19 25 4
gpt4 key购买 nike

我正在尝试在 Visual Studio 2015 中使用 ASP.NET Core MVC(预览期间的 ASP.NET 5,现在称为 ASP.NET)在单元测试下获取一些 Controller 、模型和存储库(数据访问)C# 类核心)应用程序。

我有以下结构:

   Solution
|
src
|
|-- ITConsole <- main app (ASP.NET MVC, DNX 4.5.1)
|
`-- ITConsoleTests <- What kind of project should this be?

MainApp 正在使用 DNX 4.5.1,但如果我创建一个标准的 NUnit 单元测试应用程序,它似乎只能作为经典的 .NET Framework 类库使用,目标是 .NET Framework 4.5.2,而不是作为可以与我的主程序一起使用的 Web 类库申请。

因此,以防万一它可能作为经典的 .NET 框架 Microsoft 单元测试框架项目(.NET 程序集)工作,我尝试手动查找和添加引用(通过添加引用和浏览)以获取 .NET 依赖项解决。我知道 .NET 程序集引用很遗憾是不可传递的。因此,如果 UnitTest.dll 引用了 MainApp.dll,并且 MainApp.dll 依赖于 ASP.NET MVC,以及它依赖的所有其他内容,我必须自己做。这就是我想要做的。

我在我的单元测试项目中添加了对 C:\dev\Demo\ITConsole\artifacts\bin\ITConsole\Debug\dnx451\ITConsole.dll 的引用,这样我就可以开始获取代码了编译。单元测试类可以编译,但它们不会运行,这可能是因为尝试添加对 ASP.NET 的引用一团糟。

现在,即使我添加了对 Common.Logging.Core 和 Common.Logging 的引用,当我在测试资源管理器上单击“全部运行”时,我仍收到此错误:

Test Name:    TestStudyLogReadDocument
Test FullName: ITConsoleTests.ITConsoleTestStudyLog.TestStudyLogReadDocument
Test Source: C:\dev\Demo\ITConsole\ITConsoleTests\ITConsoleTestStudyLog.cs : line 52
Test Outcome: Failed
Test Duration: 0:00:00.0712058

Result StackTrace:
at Couchbase.Configuration.Client.ClientConfiguration..ctor()
at ITConsole.Repository.StudyLogRepository..ctor() in C:\dev\Demo\ITConsole\src\ITConsole\Repository\StudyLogRepository.cs:line 39
at ITConsoleTests.ITConsoleTestStudyLog.SetupDb() in C:\dev\Demo\ITConsole\ITConsoleTests\ITConsoleTestStudyLog.cs:line 30
at ITConsoleTests.ITConsoleTestStudyLog.TestStudyLogReadDocument() in C:\dev\Demo\ITConsole\ITConsoleTests\ITConsoleTestStudyLog.cs:line 53
Result Message:
Test method ITConsoleTests.ITConsoleTestStudyLog.TestStudyLogReadDocument threw exception:
System.IO.FileLoadException: Could not load file or assembly 'Common.Logging.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

(当时问这个问题...)没有任何 ASP.NET 5 MVC 预览模板可以为您生成单元测试。您甚至可以对 Shiny 的新 ASP.NET Core 应用程序进行单元测试吗?请参阅下面的屏幕截图,例如如何使用 MSTEST 在 Visual Studio 2015 中使用单元测试的正常入门方式。

No unit test for you

最佳答案

更新:xUnit 仍然是一个好主意,但这个答案现在已经过时了,因为您可以 also use the "standard" MSTEST if you want with ASP.NET core . (2016 年 6 月 1 日)我发现我仍然更喜欢 xUnit,但这是你的决定。

最新的 xUnit 说明链接:Excellent instructions that may be updated more often than this answer are found at the xUnit wiki.

IDE 解决方法:当 Visual Studio 变得愚蠢并且不会“检测”和显示时,手动查找并删除 %TEMP%\VisualStudioTestExplorerExtensions你你的测试。

截至 2016 年 5 月,随着 ASP.NET Core 1.0 RC1 最近被 RC2 取代,似乎仍然无法将标准 Microsoft 单元测试框架与 ASP.NET Core(以前称为 ASP.NET 5)一起使用,并且 xUnit 出现是 RC1 和 RC2 的不错选择。

您可以获得 XUnit.net 单元测试以使用 ASP.NET Core 1.0.0-RC1,使用官方说明] 2在 xUnit GitHub 项目中有一个特定的“.NET Core 入门”案例。

您还可以安装 xUnit 新项目模板,该模板为常规完整 .NET 和 .NET Core 提供模板化单元测试项目。点击菜单 Tools 然后 Extensions and Updates 输入 xUnit,然后找到 xUnit Test Project template 并安装 template不要安装任何 xUnit 测试运行器;你不需要它。

我已经创建了一个工作示例并将其上传到 Bitbucket :

https://bitbucket.org/wpostma/aspnet5mvc6xunitdemo

如果你没有 Mercurial , 你可以下载 ZIP来自 Bitbucket 的文件。

演示包括一项通过的测试和一项失败的测试。

快速总结:

  1. 您有 Visual Studio 2015,包括 Update 2 和“1.0.0 预览”工具(截至 2016 年 5 月最新)。

  2. 创建一个Web 类库不是一个单元测试项目

  3. 向其添加 xUnit 引用,并修复您的 project.json(示例如下)。

  4. 写下你的类(class)(下面的例子)。

  5. 在 IDE 内或 IDE 外使用测试资源管理器运行测试,输入 dnx 。测试,并检查输出(下面的示例)。

引用演示程序集和 xUnit 的 1.0.0-rc2 文件 project.json:

 {
"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"
]
}
}
}

单元测试类(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");
}

}
}

当我们使用 dnx 时 RC1 命令行的示例输出:

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

我们使用 dotnet 的 RC2 示例输出:

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.

关于c# - 如何对 ASP.NET Core Controller 或模型对象进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31730561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com