在单元测试时使用带 asp.net 内核的最小起订量

using moq with asp.net core while unit testing

我正在尝试使用 moq 和 xunit 在 asp.net 核心中编写一个非常简单的单元测试。 但是当我 运行 单元测试时出现错误。

堆栈跟踪: 在 Moq.Mock1..ctor(MockBehavior behavior, Object[] args) at Moq.Mock1..ctor(MockBehavior 行为) 在 Moq.Mock`1..ctor()

无法加载文件或程序集'System.Core, version=4.0.0'。

下面是我的代码和 project.json 文件。

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

使用 Moq. https://blogs.msdn.microsoft.com/dotnet/2016/09/27/the-week-in-net-on-net-on-orchard-2-mocking-on-core-storyteller-armello/ 的预览版本列出了与 netstandard 兼容的其他包。

您绝对不想在 imports 节点中为 netcoreapp1.0 定位应用程序添加 net45x。该应用程序很可能无法加载程序集(就像您的情况一样),或者 运行 在 运行 时间缺少 API。