Getting Could not load file or assembly 'System.Core, Version=4.0.0.0..' While doing unit testing in asp.net core with moq

时间秒杀一切 提交于 2019-12-08 04:34:21

问题


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.

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

回答1:


Had the same issue.

Solved it by several changes:

  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)

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


来源:https://stackoverflow.com/questions/39843015/getting-could-not-load-file-or-assembly-system-core-version-4-0-0-0-while-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!