在使用moq在asp.net核心中进行单元测试时,无法加载文件或程序集"System.Core,版本= 4.0.0.0 .." [英] Getting Could not load file or assembly 'System.Core, Version=4.0.0.0..' While doing unit testing in asp.net core with moq

查看:331
本文介绍了在使用moq在asp.net核心中进行单元测试时,无法加载文件或程序集"System.Core,版本= 4.0.0.0 .."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个非常简单的单元测试.当我运行测试时,出现异常无法加载文件或程序集'无法加载文件或程序集'System.Core,版本= 4.0.0.0.'在添加"Microsoft.NETCore之后,此错误开始出现.我的Project.json文件中的"Portable.Compatibility":"1.0.1" .但这是必需的,否则我不能在单元测试中使用lambda表达式.

I am having a very simple unit test. when I run the test I get the exception Could not load file or assembly 'Could not load file or assembly 'System.Core, Version=4.0.0.0..' This error started appearing after I added "Microsoft.NETCore.Portable.Compatibility": "1.0.1" in my Project.json file. But it is required or else I cannot use lambda Expression in unit testing.

我的单元测试中没有编译时错误.我确定我的测试没有问题,这是project.json文件缺少某些内容.下面是我的测试.

There is no compile time error in my unit test.I am sure there is nothing wrong with my test it is the project.json file missing something . below is my test.

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"
}

推荐答案

有相同的问题.

通过若干更改解决了该问题:

Solved it by several changes:

  1. 将依赖关系从Moq更改为Moq.netcore(您还需要添加自定义nuget提要" 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的引用(因为未引用,但出于某些原因需要)
  1. Changed dependency from Moq to Moq.netcore (you also need to add custom nuget feed "https://www.myget.org/F/aspnet-contrib/api/v3/index.json", found it in http://dotnetliberty.com/index.php/2016/02/22/moq-on-net-core/)
  2. Removed imported dnx451 (net451 in your case), this answer helped me a lot: https://stackoverflow.com/a/39856247/1546582
  3. Removed link to compatibility project (you don't need it with moq.netcore)
  4. Add reference to System.Diagnostics.TraceSource (cause it isn't referenced, but needed for some reason)

我的最终project.json如下:

My final project.json looks like:

{
  "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"
        }
      }
    }
  }
}

这篇关于在使用moq在asp.net核心中进行单元测试时,无法加载文件或程序集"System.Core,版本= 4.0.0.0 .."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆