1

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"
}
maxspan
  • 13,326
  • 15
  • 75
  • 104
  • 1
    I remember when I used to get that exact error, the only way I could fix it was if I launch VS (my IDE) with Administrator privileges. – Rook Oct 04 '16 at 02:09
  • It still fails. – maxspan Oct 04 '16 at 02:19
  • Unlucky; it's a pretty generic error with a load of different variables affecting the issue ... Can you try moving a small portion of your code to a brand new project with all the dependencies you need and try again? – Rook Oct 04 '16 at 03:10
  • Could you post project.json files for both test and tested projects? – qbik Oct 04 '16 at 03:18
  • which platform do you target? which version of moq are you using? using the .net core runner or normal runner? Questions over questions ;) – Tseng Oct 04 '16 at 03:18
  • I have added my project.json file – maxspan Oct 04 '16 at 03:25
  • @maxspan Did you ever resolve this? I feel like I'm asking this question a lot on SO recently with regards to .net core questions! – Tom Apr 21 '17 at 11:10
  • Yes I fixed it. But now I am using visual studio 2017 so I dont use project.json file. – maxspan Apr 23 '17 at 23:12
  • 1
    @maxspan Ah ok. I'm using VS2017 and still get issues with this missing reference, even though I'm not using Moq. – Tom May 03 '17 at 08:16

1 Answers1

0

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"
        }
      }
    }
  }
}
Community
  • 1
  • 1
VorobeY1326
  • 782
  • 10
  • 25