在单元测试时使用带有asp.net核心的moq [英] using moq with asp.net core while unit testing
问题描述
我正在尝试使用moq和xunit在asp.net核心中编写一个非常简单的单元测试. 但是运行单元测试时出现错误.
I am trying to write a very simple unit test in asp.net core using moq and xunit. But I am getting a error when I run the unit test.
StackTrace:
在Moq.Mock 1..ctor(MockBehavior behavior, Object[] args)
at Moq.Mock
1..ctor处(MockBehavior行为)
在Moq.Mock`1..ctor()
StackTrace:
at Moq.Mock1..ctor(MockBehavior behavior, Object[] args)
at Moq.Mock
1..ctor(MockBehavior behavior)
at Moq.Mock`1..ctor()
无法加载文件或程序集'System.Core,version = 4.0.0'.
Could not load file or assembly 'System.Core, version=4.0.0'.
下面是我的代码和project.json文件.
Below is my code and project.json file.
{
"version": "0.1.0-*",
"testRunner": "xunit",
"dependencies": {
"Moq": "4.5.22",
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"IntegraPay.Domain": {
"version": "1.0.0-*",
"target": "project"
},
"Integrapay.RegistrationApplication": {
"version": "",
"target": "project"
},
"Microsoft.NETCore.Portable.Compatibility": "1.0.1"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"netcoreapp1.0",
"net45",
"net451"
],
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
}
}
}
}
[Fact]
public async void GetAttributes()
{
Mock<IRegistrationRepository> repo = new Mock<IRegistrationRepository>();
RegistrationManager manager = new RegistrationManager(repo.Object);
var item = await manager.CreateModel("1234");
}
推荐答案
使用起订量.
Use the preview build of Moq. https://blogs.msdn.microsoft.com/dotnet/2016/09/27/the-week-in-net-on-net-on-orchard-2-mocking-on-core-storyteller-armello/ lists other packages that are compatible with netstandard
.
您永远不想在netcoreapp1.0
定位应用程序的imports
节点中添加net45x
.该应用程序很可能无法加载程序集(例如您的情况),或者将在运行时运行到缺少的API中.
You never want to add net45x
in the imports
node for a netcoreapp1.0
targeting application. The application will most likely fail to load assemblies (like in your case) or will run in to missing APIs at runtime.
这篇关于在单元测试时使用带有asp.net核心的moq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!