When running unit tests against my Asp.Net Core Web Application (.NET Framework) 4.6 I get the following error:
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.
Here is the project.json for my Asp.Net Core Web Application (.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%" ]
}
}
Here is the project.json for my unit test project, which is a .NET Core class library targeting .NET Framework 4.6:
{
"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.