【问题标题】:using moq with asp.net core while unit testing在单元测试时使用最小起订量和 asp.net 核心
【发布时间】:2016-10-04 06:33:10
【问题描述】:

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

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

无法加载文件或程序集“System.Core,版本=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");
        }

【问题讨论】:

    标签: asp.net unit-testing moq asp.net-core-mvc project.json


    【解决方案1】:

    使用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。

    【讨论】:

    • 这意味着谁想要使用最小起订量和 asp.net 核心 1.0 请使用最小起订量版本 4.6.38-alpha。由于某种原因,稳定版 4.5.22 将无法正常工作。
    • 不知道这些导入是如何真正起作用的,但删除 dnx451 为我解决了它
    猜你喜欢
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多