[英]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.