boggy
boggy

Reputation: 4027

Assembly.LoadFrom doesn't load dll from bin\debug when running unit tests with resharper

I am using VS 2015 Update 2 with Resharper Ultimate 2016.1 and I have this weird issue.

I have a test project called Test which references two projects, Model and Persistence. The model project contains nhibernate entity classes and the Persistence project contains *.hbm.xml files. They were generated with llblgenpro 4.2. I am using nhibernate 4.0.4.

I initialize NHibernate with this call:

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

When I run one of my test cases the nhibernate initialization fails with this exception:

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: 

If I copy the persistence.dll to the C:\Users\costa\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14 folder, the test case works fine. persistence.dll is in the C:/projects/csharp/Test/bin/Debug folder because the persistence project is referenced in the test project.

This all worked fine in VS 2013 with nhibernate 3.3.1. Also I got all the dll versions to align using the assemblybinding elements in the test project app.config file.

My projects target .Net 4.6 and I use nunit 3.2.1.

I found this:

Resharper runs UnitTest from different location

However, in my case 'Shadow-copy assemblies being tested' is turned off, Use separate AppDomain for each assembly with tests is also turned off. Run Tests from is set to Project output folder.

Any ideas what could cause this?

Thanks

An update: If I do this it works:

  string path = Assembly.GetExecutingAssembly().Location;
  string directory = Path.GetDirectoryName(path);

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

Update 2. My project uses the Sharp Architecture library. NHibernateSession is a class that belongs to this library.

Upvotes: 0

Views: 756

Answers (1)

citizenmatt
citizenmatt

Reputation: 18583

This is likely a change in NUnit 3, which no longer changes the current directory to the location of the assembly under test. I believe this is because it can run multiple assemblies in a single test run - so which directory would be best?

As per the NUnit 3 Breaking Changes document, you can use TestContext.CurrentContext.TestDirectory to locate the directory that contains the assembly under test.

Upvotes: 2

Related Questions