获取无法加载文件或程序集' System.Core,Version = 4.0.0.0 ..'在使用moq在asp.net核心中进行单元测试时

时间:2016-10-04 01:48:40

标签: c# asp.net unit-testing asp.net-core project.json

我正在进行一项非常简单的单元测试。当我运行测试时,我得到异常无法加载文件或程序集'无法加载文件或程序集' System.Core,Version = 4.0.0.0 ..'在我的Project.json文件中添加" Microsoft.NETCore.Portable.Compatibility":" 1.0.1" 后,此错误开始出现。但它是必需的,否则我不能在单元测试中使用lambda Expression。

我的单元测试中没有编译时错误。我确定我的测试没有任何问题,因为project.json文件缺少某些东西。以下是我的考试。

Mock<RegistrationManager> manager = new Mock<RegistrationManager>();
            Mock<RegistrationModel> model = new Mock<RegistrationModel>();
            var value = manager.Setup(a => a.GetDataFieldValuesForModel(model.Object, CommandType.next)).ReturnsAsync(new RegistrationModel { hasError = false, FormId="123",LeadId="345" });


{
  "version": "0.1.0-*",
  "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": [
        "net451"
      ],
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  },
  "testRunner": "xunit"
}

1 个答案:

答案 0 :(得分:0)

有同样的问题。

通过几处改动解决了这个问题:

  1. 将依赖从Moq更改为Moq.netcore(您还需要添加自定义nuget Feed“https://www.myget.org/F/aspnet-contrib/api/v3/index.json”,在http://dotnetliberty.com/index.php/2016/02/22/moq-on-net-core/中找到它)
  2. 删除了导入的dnx451(在你的情况下为net451),这个答案对我有很大帮助:https://stackoverflow.com/a/39856247/1546582
  3. 删除了与兼容项目的链接(您不需要使用moq.netcore)
  4. 添加对System.Diagnostics.TraceSource的引用(因为它未被引用,但出于某种原因需要)
  5. 我的最终project.json看起来像:

    {
      "version": "1.0.0-*",
    
      "dependencies": {
        "NUnit": "3.5.0",
        "dotnet-test-nunit": "3.4.0-beta-2",
        "MyTestingProject": "1.0.0-*",
        "Moq.netcore": "4.4.0-beta8",
        "System.Diagnostics.TraceSource": "4.0.0" 
      },
      "testRunner": "nunit",
    
      "frameworks": {
        "netcoreapp1.0": {
          "imports": [
            "netcoreapp1.0",
            "dnxcore50"
          ],
          "dependencies": {
            "Microsoft.NETCore.App": {
              "version": "1.0.1",
              "type": "platform"
            }
          }
        }
      }
    }