Assembly.LoadFrom 在使用 resharper 运行单元测试时不会从 bin\debug 加载 dll



我正在使用VS 2015 Update 2和Resharper Ultimate 2016.1,我遇到了这个奇怪的问题。

我有一个名为 Test 的测试项目,它引用了两个项目,模型和持久性。模型项目包含 nhibernate 实体类,持久性项目包含 *.hbm.xml 文件。它们是用llblgenpro 4.2生成的。我正在使用 nhibernate 4.0.4。

我用这个调用初始化NHibernate:

  NHibernateSession.Init(
    new SimpleSessionStorage(), 
    new string[] { "Persistence.dll", "Model.dll" });

当我运行我的一个测试用例时,nhibernate初始化失败,出现以下异常:

System.IO.FileNotFoundException was unhandled by user code
  FileName=file:///C:UserscostaAppDataLocalJetBrainsInstallationsReSharperPlatformVs14Persistence.dll
  FusionLog==== Pre-bind state information ===
LOG: Where-ref bind. Location = C:UserscostaAppDataLocalJetBrainsInstallationsReSharperPlatformVs14Persistence.dll
LOG: Appbase = file:///C:/projects/csharp/Test/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:UserscostaAppDataLocalTemps0hjyhsk.jq1a3514fde-acb9-4c62-a0ce-a586f8202f35.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFramework64v4.0.30319configmachine.config.
LOG: Attempting download of new URL file:///C:/Users/costa/AppData/Local/JetBrains/Installations/ReSharperPlatformVs14/Persistence.dll.
  HResult=-2147024894
  Message=Could not load file or assembly 'file:///C:UserscostaAppDataLocalJetBrainsInstallationsReSharperPlatformVs14Persistence.dll' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  StackTrace:
       at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
       at System.Reflection.Assembly.LoadFrom(String assemblyFile)
       at SharpArch.NHibernate.NHibernateSession.<>c__DisplayClass36_0.<CreateSessionFactoryFor>b__0(MappingConfiguration m) in C:worksharp-archSolutionsSharpArch.NHibernateNHibernateSession.cs:line 412
       at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
  InnerException: 

如果我将持久性.dll复制到 C:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14 文件夹,测试用例工作正常。 持久性.dll位于 C:/projects/csharp/Test/bin/Debug 文件夹中,因为持久性项目在测试项目中被引用。

这一切都在VS 2013的nhibernate 3.3.1中运行良好。此外,我还使用测试项目 app.config 文件中的程序集绑定元素对齐所有 dll 版本。

我的项目面向.Net 4.6,我使用nunit 3.2.1。

我发现这个:

Resharper 从不同位置运行 UnitTest

但是,在我的情况下,"正在测试的卷影副本程序集"已关闭,对每个具有测试的程序集使用单独的 AppDomain 也处于关闭状态。 "从以下位置运行测试"设置为"项目输出文件夹"。

任何想法可能导致这种情况?

谢谢

更新:如果我这样做,它可以工作:

  string path = Assembly.GetExecutingAssembly().Location;
  string directory = Path.GetDirectoryName(path);
  NHibernateSession.Init(
    new SimpleSessionStorage(), 
    new string[] { directory + "\Persistence.dll", directory + "\Model.dll" });

更新 2.我的项目使用夏普架构库。NHibernateSession 是属于此库的类。

这可能是 NUnit 3 中的更改,它不再将当前目录更改为受测程序集的位置。我相信这是因为它可以在一次测试运行中运行多个程序集 - 那么哪个目录最好?

根据 NUnit 3 中断性更改文档,可以使用TestContext.CurrentContext.TestDirectory查找包含受测程序集的目录。

相关内容

最新更新