using moq with asp.net core while unit testing

两盒软妹~` 提交于 2019-12-14 02:23:45

问题


I am trying to write a very simple unit test in asp.net core using moq and xunit. But I am getting a error when I run the unit test.

StackTrace: at Moq.Mock1..ctor(MockBehavior behavior, Object[] args) at Moq.Mock1..ctor(MockBehavior behavior) at Moq.Mock`1..ctor()

Could not load file or assembly 'System.Core, version=4.0.0'.

Below is my code and project.json file.

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


 [Fact]
        public async void GetAttributes()
        {
            Mock<IRegistrationRepository> repo = new Mock<IRegistrationRepository>();
            RegistrationManager manager = new RegistrationManager(repo.Object);
            var item = await manager.CreateModel("1234");
        }

回答1:


Use the preview build of Moq. https://blogs.msdn.microsoft.com/dotnet/2016/09/27/the-week-in-net-on-net-on-orchard-2-mocking-on-core-storyteller-armello/ lists other packages that are compatible with netstandard.

You never want to add net45x in the imports node for a netcoreapp1.0 targeting application. The application will most likely fail to load assemblies (like in your case) or will run in to missing APIs at runtime.



来源:https://stackoverflow.com/questions/39845582/using-moq-with-asp-net-core-while-unit-testing

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