c# - 在 Asp.Net Core Web 应用程序 (.NET Framework) 4.6 上运行单元测试时出错

标签 c# asp.net unit-testing

在针对我的 Asp.Net Core Web 应用程序 (.NET Framework) 4.6 运行单元测试时,出现以下错误:

Test Name:  Test1
Test FullName:  UnitTest.Class1.Test1
Test Outcome:   Failed
Test Duration:  0:00:00.015

Result StackTrace:  at UnitTest.Class1.Test1()
Result Message: System.BadImageFormatException : Could not load file or
assembly 'MyWebApplication, Version=1.0.0.0, Culture=neutral, 
PublicKeyToken=null' or one of its dependencies. An attempt was made to 
load a program with an incorrect format.

我的单元测试应用程序是一个面向 .NET Framework 4.6 的 .NET Core 库。

这是我的 Asp.Net Core Web 应用程序(.NET Framework)4.6 的 project.json:

{
  "dependencies": {

    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Configuration": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.DependencyInjection": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
  },

  "tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-    final"
  },

  "frameworks": {
    "net46": {
      "dependencies": {
        "MyWebApplication": {
          "target": "project"
        }
      },
      "frameworkAssemblies": {
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config",
      "Views",
      "appsettings.json"
    ]
  },

  "scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder     %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

这是我的单元测试项目的 project.json,它是一个面向 .NET Framework 4.6 的 .NET Core 类库:

{
    "version": "1.0.0-*",

    "dependencies": {
        "MyWebApplication": "1.0.0-*",
        "NUnit": "3.5.0",
        "NUnit.Runners": "2.6.4",
        "NUnit3TestAdapter": "3.5.0"
    },

    "frameworks": {
        "net46": {
        }
    },

    "testRunner": "NUnit"
}

这是我正在运行的整个测试:

using MyWebApplication.Controllers;
using NUnit.Framework;

namespace UnitTest
{
    [TestFixture]
    public class Class1
    {
        [Test]
        public void Test1()
        {
            Dummy dummy = new Dummy();
        }
    }
}

这是整个类(class):

namespace MyWebApplication.Controllers
{
    public class Dummy
    {
        public Dummy()
        {

        }
    }
}

最后一个细节,我的 Asp.Net Core Web Application (.NET Framework) 4.6 引用了 2 个其他 .NET 类库项目,1 个是引用 .NET Framework 4.5 的 Entity Framework (5.0) 项目,第二个是一个类以 .NET Framework 4.6 为目标的库。

最佳答案

我找到了答案!

我将我的单元测试项目创建为 .NET Core dll,然后从 project.json 中注释掉 .netappcore 框架引用并将其替换为 net46:

{
    "version": "1.0.0-*",

    "dependencies": {
        "NETStandard.Library": "1.6.0",
        "NUnit": "3.5.0",
        "NUnit.Runners": "3.5.0",
        "NUnit3TestAdapter": "3.5.1",
        "xunit": "2.1.0",
        "dotnet-test-xunit": "1.0.0-rc2-build10025",
        "xunit.runner.visualstudio": "2.1.0",
        "xunit.runners": "2.0.0",
        "FakeItEasy": "2.3.1",
        "MyWebApplication": "1.0.0-*",
        "Moq": "4.5.28",
        "Microsoft.Framework.Configuration.Json": "1.0.0-beta8"
    },

    "frameworks": {
        "net46": {
            "dependencies": {
                "AnotherProject.UnitTest": {
                    "target": "project"
                }
            }
        }
    },
    "testRunner": "xunit"
}

然后我安装了 xunit。

然后我不得不重新安装 MyWebApplication 的所有依赖项。其中之一是 1.0.1,尽管在 packages 文件夹中显示为 1.0.0。

完成这些事情后,我就可以开始单元测试了。

关于c# - 在 Asp.Net Core Web 应用程序 (.NET Framework) 4.6 上运行单元测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40288679/

相关文章:

c# - 从 Type 获取新的对象实例并在编译时访问它

c# - 如何将值添加到 list<valuepair> 对象

c# - 为什么邮件主题在 MailGun 的 HTTP 帖子中重复?

perl - 我应该使用哪个 Perl 测试模块?

android - Android Studio : "not mocked" error 单元测试

c# - 百万对象创建的性能建议

c# - 如何处理 Web 服务中使用的冗余类

c# - 我不明白这个短语是如何在 c# 中计算的

mysql - 不允许通过 IISExpress 连接到 Mysql 服务器

Android 单元测试 - 如何在与应用程序相同的项目中运行测试?