模拟asp.net核心5.0的框架

use*_*278 26 c# asp.net moq asp.net-core

我最近安装了Visual Studio 2015并启动了一个带有网站和asp类库的项目,该库将包含网站的单元测试.我通常使用Moq进行模拟,但尝试不同的模拟框架我并不陌生.我遇到的问题是我添加了Moq作为单元测试项目的参考并开始使用它.在我尝试编译之前,一切似乎都很好.

当我编译时,我收到一条错误消息:

ASP.NET Core 5.0 error CS0246: The type or namespace name 'Moq' could not be found (are you missing a using directive or an assembly reference?)

我注意到我可以在代码视图中切换ASP.NET 5.0和ASP.NET Core 5.0,当选择ASP.NET Core 5.0时,我会在选择ASP.NET 5.0时遇到错误但没有错误.我试着寻找答案,但我没有运气.

问题是Moq不能与vnext一起使用,我应该使用不同的框架(如果这样有效吗?)或者我能以某种方式解决这个问题吗?我的project.json:

{
"version": "1.0.0-*",
"dependencies": {
    "Web": "1.0.0-*",
    "Xunit.KRunner": "1.0.0-beta1",
    "xunit": "2.0.0-beta5-build2785",
    "Moq": "4.2.1409.1722"
},

"frameworks": {
    "aspnet50": {
        "dependencies": {
        }
    },
    "aspnetcore50": {
        "dependencies": {
        }
    }
},
"commands": {
    "test": "Xunit.KRunner"
}

}
Run Code Online (Sandbox Code Playgroud)

小智 26

Microsoft的ASP.NET团队为内部使用创建了一个特殊版本的Moq.你可以在asp.net github页面上找到一个fork项目.

怎么用?

  • 在project.json中注册Moq依赖项:

    {
        "frameworks": {
            "dnx451": {
                "dependencies": {
                    "Moq": "4.2.1312.1622"
                }
            },
            "dnxcore50": {
                "dependencies": {
                    "moq.netcore": "4.4.0-beta8"
                }
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在项目主目录中创建NuGet.config文件.

    <?xml version="1.0" encoding="utf-8"?>
        <configuration>
            <packageSources>
                <add key="AspNetVNext" value="https://www.myget.org/F/aspnetcidev/api/v3/index.json" />
                <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
            </packageSources>
        </configuration>
    
    Run Code Online (Sandbox Code Playgroud)

并使用Moq!:)

  • 为了找到moq.netcore,我必须将AspNetVNext值设置为"https:// www.myget.org/F/aspnet-contrib/api/v3/index.json". (2认同)
  • 这个答案开头的[asp.net github page](https://github.com/aspnet/moq4)链接被破坏了. (2认同)

Luc*_*lli 8

我想到目前为止,一个与asp.net core 5.0一起使用的Moq版本在nuget feed中不可用,但我认为你可以使用Asp.Net 5.0.

  • 我删除了"aspnetcore50":{"dependencies":{}}它开始工作了 (2认同)