gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:03 24 4
gpt4 key购买 nike

在针对我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com