简体   繁体   English

单元测试 - 组装加载失败

[英]Unit test - Assemly load failed

I have a Visual studio project (containing NUnit tests) with Platform target: Any CPU (Debug and Release).我有一个 Visual Studio 项目(包含 NUnit 测试),平台目标:任何 CPU (调试和发布)。 This projects references a dll (Reference.dll) with following Configuration:此项目引用具有以下配置的 dll (Reference.dll):

(from ILSpy)
// Architecture: AnyCPU (64-bit preferred)
// This assembly contains unmanaged code.
// Runtime: .NET 4.0

[assembly: CLSCompliant(true)]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyProduct("DotNet")]
[assembly: AssemblyTitle("DotNet")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: TargetFramework(".NETFramework,Version=v4.5", FrameworkDisplayName = ".NET Framework 4.5")]
[assembly: SecurityRules(SecurityRuleSet.Level1)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]

Upon building the project, Tests.dll is generated with following configuration.构建项目后,将使用以下配置生成 Tests.dll。

(from ILSpy)
// Architecture: AnyCPU (64-bit preferred)
// Runtime: .NET 4.0

[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTrademark("")]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: ComVisible(false)]
[assembly: Guid("37AAB39F-D11B-4C7D-887C-A056A63177A3")]
[assembly: TargetFramework(".NETFramework,Version=v4.5", FrameworkDisplayName = ".NET Framework 4.5")]

I use Resharper to run the NUnit tests with following preferences:我使用 Resharper 运行具有以下首选项的 NUnit 测试:

Default platform architecture: Automatic based on test project's target platform默认平台架构:自动基于测试项目的目标平台

Default .NET framework version: Automatic based on test projet's target platform默认 .NET 框架版本:自动基于测试项目的目标平台

When a test case is executed and any method from Reference.dll is used, I get following exception: System.BadImageFormatException: Could not load file or assembly '' or one of its dependencies.当执行测试用例并使用 Reference.dll 中的任何方法时,出现以下异常: System.BadImageFormatException:无法加载文件或程序集“”或其依赖项之一。 An attempt was made to load a program with an incorrect format.试图加载格式不正确的程序。

When I change the target platform of Tests.dll to x86 , it works.当我将 Tests.dll 的目标平台更改为x86时,它可以工作。 Could anyone explain me why Any CPU for both the platforms is not working.谁能解释一下为什么这两个平台的任何 CPU都不起作用。

This seems to be failing because the assembly that is being tested (not the one with your tests), is 32 bit.这似乎失败了,因为正在测试的程序集(不是您测试的程序集)是 32 位的。 If the test project is set to AnyCPU, it will run as 64 bit, and fail to load the assembly under test.如果测试项目设置为 AnyCPU,它将以 64 位运行,并且无法加载被测程序集。

You need to set your test project to have the same bit-ness as the assembly under test, or its dependencies.您需要将测试项目设置为与被测程序集或其依赖项具有相同的位数。 ReSharper only looks at the test assembly when deciding if it should run as 32 or 64 bit. ReSharper 在决定它应该以 32 位还是 64 位运行时仅查看测试程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM