在Asp上运行单元测试时出错.. Net Core Web应用程序.. NET Framework)
本文关键字:Web Core 应用程序 NET Framework Net 出错 Asp 运行 单元测试 | 更新日期: 2023-09-27 18:15:27
在对Asp运行单元测试时。. Net Core Web应用程序。我得到以下错误:
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 Core Library,目标是。net Framework 4.6。
这是项目。我的Asp. json。. Net Core Web应用程序。. NET Framework) 4.6:
{
"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%" ]
}
}
这是项目。. 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();
}
}
}
这是整个类:
namespace MyWebApplication.Controllers
{
public class Dummy
{
public Dummy()
{
}
}
}
最后一个细节,我的Asp。. Net Core Web应用程序。.NET Framework 4.6引用了2个其他。NET类库项目,1是一个实体框架(5.0)项目,引用了。NET Framework 4.5,第二个是一个针对。NET Framework 4.6的类库。
我找到答案了!
我将单元测试项目创建为。net Core dll,然后注释掉项目中的。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。
一旦这些事情完成了,我就可以开始单元测试了。