简体   繁体   English

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

[英]Error Running Unit Tests on Asp.Net Core Web Application (.NET Framework) 4.6

When running unit tests against my Asp.Net Core Web Application (.NET Framework) 4.6 I get the following error: 当针对我的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.

My unit test application is a .NET Core Library targeting .NET Framework 4.6. 我的单元测试应用程序是面向.NET Framework 4.6的.NET Core库。

Here is the project.json for my Asp.Net Core Web Application (.NET Framework) 4.6: 这是我的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%" ]
  }
}

Here is the project.json for my unit test project, which is a .NET Core class library targeting .NET Framework 4.6: 这是我的单元测试项目的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"
}

Here is the entire test I'm running: 这是我正在运行的整个测试:

using MyWebApplication.Controllers;
using NUnit.Framework;

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

Here is the entire class: 这是整个课程:

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

        }
    }
}

One last detail, my Asp.Net Core Web Application (.NET Framework) 4.6 references 2 other .NET class library projects, 1 is an Entity Framework (5.0) project that references .NET Framework 4.5 and the second is a class library that targets .NET Framework 4.6. 最后一个细节是我的Asp.Net核心Web应用程序(.NET Framework)4.6引用了另外两个.NET类库项目,一个是引用.NET Framework 4.5的实体框架(5.0)项目,第二个是针对目标的类库。 .NET Framework 4.6。

I found the answer! 我找到了答案!

I created my unit testing project as a .NET Core dll, then commented out the .netappcore framework reference from the project.json and replaced it with net46: 我创建了一个作为.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"
}

I then installed xunit. 然后,我安装了xunit。

Then I had to reinstall all the dependencies with MyWebApplication. 然后,我必须使用MyWebApplication重新安装所有依赖项。 One of them was 1.0.1, even though in the packages folder it said 1.0.0. 其中一个是1.0.1,即使在packages文件夹中显示为1.0.0。

Once these things were done, I was able to begin unit testing. 完成这些操作后,我就可以开始单元测试了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net核心应用程序定位框架4.6无法解析.NETFramework 4.6版 - ASP.net core application targetting framework 4.6 unable to resolve for .NETFramework version 4.6 将文件从 ASP.NET MVC 4.6 应用程序发布到 ASP.NET Core Web API - Posting a file from ASP.NET MVC 4.6 application to ASP.NET Core Web API 如何对ASP.NET核心Web应用程序(.Net Framework)进行单元测试? - How do you unit test ASP.NET Core Web Application (.Net Framework)? 在完整的.NET Framework 4.6中使用ASP.NET Core功能 - Using ASP.NET Core features in full .NET Framework 4.6 ASP.NET核心Web应用程序(.NET Framework)TagHelper Intellisense - ASP.NET Core Web Application (.NET Framework) TagHelper Intellisense 引用.NET Framework 4.6程序集的ASP.Net 5应用程序 - ASP.Net 5 Application referencing .NET Framework 4.6 assembly 在集成测试中运行 asp.net web api 应用程序 - running asp.net web api application in integration tests ASP.NET 5 RC Web API +框架4.6 RC库 - ASP.NET 5 RC Web API + Framework 4.6 RC Lib ASP.NET Web 应用程序(.NET 框架)中的 asp.net 核心 SignalR 配置 - asp.net core SignalR configuration in ASP.NET Web Application (.NET framework) ASP.Net Core 单元测试“与框架/平台设置不匹配” - ASP.Net Core unit tests "do not match framework/platform settings"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM