0

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

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

我用这个调用初始化 NHibernate:

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

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

System.IO.FileNotFoundException was unhandled by user code
  FileName=file:///C:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\Persistence.dll
  FusionLog==== Pre-bind state information ===
LOG: Where-ref bind. Location = C:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\Persistence.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:\Users\costa\AppData\Local\Temp\s0hjyhsk.jq1\a3514fde-acb9-4c62-a0ce-a586f8202f35.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.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:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\Persistence.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:\work\sharp-arch\Solutions\SharpArch.NHibernate\NHibernateSession.cs:line 412
       at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
  InnerException: 

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

这一切在 VS 2013 中使用 nhibernate 3.3.1 运行良好。此外,我使用测试项目 app.config 文件中的 assemblybinding 元素使所有 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. 我的项目使用Sharp Architecture 库。NHibernateSession 是属于这个库的一个类。

4

1 回答 1

2

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

根据NUnit 3 Breaking Changes 文档,您可以使用它TestContext.CurrentContext.TestDirectory来定位包含被测程序集的目录。

于 2016-04-26T13:47:10.387 回答