2016-10-27 34 views
1

運行時對我的Asp.Net核心Web應用程序(.NET Framework)的單元測試4.6我得到以下錯誤:錯誤的Asp.Net核心的Web應用程序運行單元測試(.NET框架)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核心庫。

這裏是我的Asp.Net核心Web應用程序(.NET框架)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%" ] 
    } 
} 

這裏是我的單元測試項目,這是一個.NET核心的project.json類庫目標的.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" 
} 

這裏是我運行整個測試:

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核心Web應用程序(.NET框架)4.6參考文獻2其他.NET類庫項目,1是一個實體框架(5.0)項目引用.NET Framework 4.5,第二個是面向.NET Framework 4.6的類庫。

回答

0

我找到了答案!

我創建單元測試項目作爲.NET的核心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,即使在包文件夾中它說1.0.0。

一旦完成這些事情,我就可以開始進行單元測試。

相關問題