更多细节:
我的.net解决方案看起来像这样:
- MainProject(exe)[依赖于'Class1Project'并在运行时使用'AssemblyLoaderProject'加载它]。 'Class1Project'已设置为“复制本地= false”,而'AssemblyLoaderProject'已设置为“复制本地= true” - Class1Project(dll) - AssemblyLoaderProject(使用AppDomain.CurrentDomain.AssemblyResolve和Assembly.LoadFrom在运行时加载和解析依赖项程序集) - UnitTestsProject(Nunit或MSTest)
在单元测试项目中,我试图测试'Class1Project',并希望将其配置设置为与MainProject相同。
也就是说,UnitTestProject还引用具有“copy local = false”的'Class1Project'和具有“copy local = true”的'AssemblyLoaderProject',并使用它来在运行时加载程序集。
但由于某种原因,单元测试无法执行,运行器引发FileNotFoundException指定无法解析'Class1Project'程序集。
尝试调试它,我看到测试运行器甚至没有到达代码,告诉AssemblyLoaderProject加载程序集。
测试代码如下:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Loader.LoadAssemblies();
Class1 cls = new Class1();
Assert.IsTrue(true);
}
}
当我尝试使用VS2012测试运行器在MSTest上调试时,出现了以下错误提示:
Test Name: TestMethod1
Test FullName: UnitTestProject1.UnitTest1.TestMethod1
Test Source: c:\Users\user\Documents\Visual Studio 2012\Projects\ClassLibrary1\UnitTestProject1\UnitTest1.cs : line 13
Test Outcome: Failed
Test Duration: 0:00:00.1177608
Result Message:
Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception:
System.IO.FileNotFoundException: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.=== Pre-bind state information ===
LOG: User = \user
LOG: DisplayName = ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : UnitTestProject1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug/ClassLibrary1.DLL.
LOG: Attempting download of new URL file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug/ClassLibrary1/ClassLibrary1.DLL.
LOG: Attempting download of new URL file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug/ClassLibrary1.EXE.
LOG: Attempting download of new URL file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug/ClassLibrary1/ClassLibrary1.EXE.
Result StackTrace: at UnitTestProject1.UnitTest1.TestMethod1()
TestInitialize
之前,因此在探测发生之前LoadAssemblies
尚未运行,会抛出相同的异常。 - Mike Zboray