Error Running Unit Tests on Asp.Net Core Web Application (.NET Framework) 4.6
当对我的ASP.NET核心Web应用程序(.NET框架)4.6运行单元测试时,我得到以下错误:
1 2 3 4 5 6 7 8 9 10 | 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核心库。
以下是我的ASP.NET核心Web应用程序(.NET框架)4.6的project.json:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | { "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核心类库:
2以下是我正在运行的整个测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using MyWebApplication.Controllers; using NUnit.Framework; namespace UnitTest { [TestFixture] public class Class1 { [Test] public void Test1() { Dummy dummy = new Dummy(); } } } |
这是整个班级:
1 2 3 4 5 6 7 8 9 10 | namespace MyWebApplication.Controllers { public class Dummy { public Dummy() { } } } |
最后一个细节是,我的ASP.NET核心Web应用程序(.NET框架)4.6引用了其他2个.NET类库项目,1是引用.NET框架4.5的实体框架(5.0)项目,另一个是针对.NET框架4.6的类库。
我找到答案了!
我将单元测试项目创建为.NET核心DLL,然后从project.json中注释掉.netapcore framework引用,并将其替换为net46:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | { "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。
完成这些操作后,我就可以开始单元测试了。