- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试在 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 中使用单元测试的正常入门方式。
最佳答案
更新: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 的文件。
演示包括一项通过的测试和一项失败的测试。
快速总结:
您有 Visual Studio 2015,包括 Update 2 和“1.0.0 预览”工具(截至 2016 年 5 月最新)。
创建一个Web 类库,不是一个单元测试项目。
向其添加 xUnit 引用,并修复您的 project.json(示例如下)。
写下你的类(class)(下面的例子)。
在 IDE 内或 IDE 外使用测试资源管理器运行测试,输入 dnx 。测试
,并检查输出(下面的示例)。
{
"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/
主要思想是将 EF Core nuget 包添加到 .NET Core 库项目,然后在一堆应用程序(例如 ASP.NET Core、Win 服务、控制台应用程序)中使用该库,而无需在每个应用程序中配置
我想要实现的是编写一个简单的.net核心后台工作程序(.net core 3.1)的代码,在该工作程序作为Windows服务运行时,我在其中将数据写入SQL Server数据库(通过EF Core 3
关于 .Net Core SDK download page 二进制文件有什么用?它与安装程序有何不同? 最佳答案 二进制文件是 .NET Core 的编译代码。它们拥有运行 .NET Core 所需
.NET Core 和 Entity Framework Core 之间的区别?我们可以在 .NET Core 中使用 Entity Framework Core 吗?两者都有什么优势? 最佳答案 E
.NET Core 和 ASP.NET Core 到底有什么区别? 它们是相互排斥的吗?我听说 ASP.NET Core 是基于 .NET Core 构建的,但它也可以基于完整的 .NET 框架构建。
我对 ASP.NET Core 开发完全陌生。我正在尝试使用单个模型和 mysql 创建一个简单的 asp.net core Web api 来存储模型数据,然后我想使用 Swagger 将其作为 R
.NET Core 和 Entity Framework Core 之间的区别?我们可以在 .NET Core 中使用 Entity Framework Core 吗?两者都有什么优势? 最佳答案 E
好吧,作为一个新的 .net 开发生态系统,我有点迷失在核心工具、版本等方面。 有人可以解释我之间的区别吗 VS 2015 核心工具预览版 x - See here .NET Core/SDK 与否
我已阅读有关如何通过信号器核心集线器从后台服务向客户端发送通知的文档。如何从客户端接收到后台服务的通知? 后台服务应该只是一个单例。 public class Startup { public
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
非常简单的问题: 我正在尝试创建一个像这样的谓词构建器: var predicate = PredicateBuilder.False(); 但似乎在Net Core和EF Core中不可用。
在 .NET Core 自包含应用程序 中...我们需要在 project.json 中指定运行时 (RID) 我们希望我们的应用程序针对...发布为什么会这样? .NET Core 是跨平台的,与我
如何用 iCloud Core Data 替换我现有的 Core Data?这是我的持久商店协调员: lazy var persistentStoreCoordinator: NSPersistent
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 2 年前。 Improv
今天我正在学习新的 ASP.net 核心 API 3.1,我想将我的旧网站从 MVC4 转移到 Web API。除了一件事,一切都很好。数据库连接。在我的旧网站中,我为每个客户端(10/15 数据库)
我在 Visual Studio 2015 Update 3 和 .NET Core 1.0 中工作。我有一个 .NETCoreApp v1.0 类型的 Web API 项目。当我添加一个 .NET
我一直在尝试遵循 Ben Cull ( http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-proj
当我打开我的 vs 代码程序时,我收到以下消息: 无法找到 .NET Core SDK。 .NET Core 调试将不会启用。确保 .NET Core SDK 已安装并且在路径上。 如果我安装甚至卸载
我偶然发现了一个非常奇怪的问题。每当 Web 应用程序启动时,dotnet.exe 都会使用相当多的内存(大约 300M)。然而,当它触及某些部分时(我感觉这与 EF Core 使用有关),它会在短时
ASP.NET Core Web (.NET Core) 与 ASP.NET Core Web (.NET Framework) 有什么区别? .NET Framework 是否提供 similar
我是一名优秀的程序员,十分优秀!