我刚接触moq,在我的Asp.Net 5和MVC6测试项目中使用它时遇到了问题。
我使用NuGet安装了Moq,并安装了Moq 4.2.1510.2205。当我查看project.json时,我得到了
{
"version": "1.0.0-*",
"description": "TargetTests Class Library",
"authors": [ "Admin" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"Target": "1.0.0-*",
"xunit": "2.2.0-beta1-build3239",
"xunit.runner.console": "2.2.0-beta1-build3239",
"xunit.runner.dnx": "2.1.0-rc1-build204"
},
"commands": {
"test": "xunit.runner.dnx"
},
"frameworks": {
"dnx451": {
"dependencies": {
"Moq": "4.2.1510.2205"
}
},
"dnxcore50": {
"dependencies": {
}
}
}
}当我在cs文件中使用Moq添加时,我得到了以下错误
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'Moq' could not be found (are you missing a using directive or an assembly reference?) TargetTests.DNX Core 5.0 C:\Users\Admin\Desktop\Target\src\TargetTests\TestBase.cs 6 Active有没有人能帮我看看如何解决这个问题?我检查了StackOverfollow,并尝试了Mocking framework for asp.net core 5.0中介绍的方法,但失败了。
发布于 2016-03-20 02:50:25
这是因为您从NuGet安装的Moq版本不支持DNX平台,并且由于您的project.json文件中有一个"dnxcore50"组件,因此您的项目被配置为可以在该平台上运行。
如果您将光标悬停在对Moq的方法调用上,您应该会看到如下所示:

要解决此问题,您有两个选择:
"dnxcore50"组件(因此,您的代码只能在安装了ASP.Net 4.5.1+的环境中运行)。如果你想选择后一种方法,second answer by Lucas Pyrzyk在我试用的时候是有效的(包括他答案下面的注释)。
首先,将project.json的依赖项添加到moq.netcore库。
{
"version": "1.0.0-*",
"description": "TargetTests Class Library",
"authors": [ "Admin" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"Target": "1.0.0-*",
"xunit": "2.2.0-beta1-build3239",
"xunit.runner.console": "2.2.0-beta1-build3239",
"xunit.runner.dnx": "2.1.0-rc1-build204"
},
"commands": {
"test": "xunit.runner.dnx"
},
"frameworks": {
"dnx451": {
"dependencies": {
"Moq": "4.2.1510.2205"
}
},
"dnxcore50": {
"dependencies": {
"moq.netcore": "4.4.0-beta8"
}
}
}然后,在使用该文件的项目根目录中添加一个名为NuGet.config的文件。它的内容应该如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="AspNetVNext" value="https://www.myget.org/F/aspnet-contrib/api/v3/index.json" />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>发布于 2016-08-24 23:11:09
使用"Moq":"4.6.25-alpha"
我的project.json:
{
"version": "1.0.0-*",
"testRunner": "xunit",
"dependencies": {
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.AspNetCore.TestHost": "1.0.0",
"Moq": "4.6.25-alpha",
"Newtonsoft.Json": "9.0.1",
"System.Diagnostics.TraceSource": "4.0.0",
"System.Net.Http": "4.1.0"
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": [
"dotnet5.4",
"portable-net451+win8"
]
}
},
"buildOptions": {
"copyToOutput": {
"include": [ "xunit.runner.json" ]
}
}
}https://stackoverflow.com/questions/36102010
复制相似问题