2
votes

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.

1

1 Answers

0
votes

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:

{
    "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.

Then I had to reinstall all the dependencies with MyWebApplication. One of them was 1.0.1, even though in the packages folder it said 1.0.0.

Once these things were done, I was able to begin unit testing.