Unit tests running in NCrunch but not in Resharper
Asked Answered
T

3

5

I've got a set of unit tests that used to work very nicely in NCrunch and the Resharper 7 (in VS2012) test runner. I added SignalR and SignalR.Ninject through NuGet, and now my unit tests work in NCrunch, but no longer run in Resharper.

The TestFixtureSetUp failed with the following error:

SetUp : System.IO.FileLoadException : Could not load file or assembly 
'Ninject, Version=2.2.0.0,    Culture=neutral, PublicKeyToken=c7192dc5380945e7' 
or one of its dependencies. The located assembly's manifest definition does 
not match the assembly reference. (Exception from HRESULT: 0x80131040)

at ProjectName.Infrastructure.SiteResolver.BindSignalR(IKernel kernel)
at ProjectName.Infrastructure.SiteResolver.RegisterServices(IKernel kernel) in SiteResolver.cs: line 29
at ProjectName.Tests.Unit.DataTests.Init() in DataTests.cs: line 48

In App.Config, I have

<runtime>
  <assemblyBinding>
    <dependentAssembly>
      <assemblyIdentity name="Ninject" publicKeyToken="c7192dc5380945e7" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

The equivalent in Web.Config works in the actual MVC project.

Any ideas how to fix it?

Update

I've tried with shadow-copying on and off, with no difference in outcome.

Update

Output from log file generated from Jim Skim's answer:

*** Assembly Binder Log Entry  (15/10/2012 @ 16:43:47) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Program Files (x86)\JetBrains\ReSharper\v7.0\Bin\JetBrains.ReSharper.TaskRunner.CLR4.MSIL.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = ######\simon
LOG: DisplayName = Ninject, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7
 (Fully-specified)
LOG: Appbase = file:///Z:/ProjectName/ProjectName.Tests.Unit/bin/Debug
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = ProjectName.Tests.Unit
Calling assembly : ProjectName.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: Z:\ProjectName\ProjectName.Tests.Unit\bin\Debug\ProjectName.Tests.Unit.dll.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Ninject, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///Z:/ProjectName/ProjectName.Tests.Unit/bin/Debug/Ninject.DLL.
LOG: Assembly download was successful. Attempting setup of file: Z:\ProjectName\ProjectName.Tests.Unit\bin\Debug\Ninject.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Ninject, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Thesis answered 15/10, 2012 at 13:33 Comment(3)
Which version of Visual Studio? Which version of R#?Rubbing
Resharper 7 (in VS2012). Updated question; thanks.Thesis
VS2012 Update 1 is supposed to have fixed this bug, so assembly binding redirection should now work in unit test projects.Singsong
T
2

From this link (mentioned originally by @bryanbcook), although the Resharper 7.1 upgrade didn't work, one of the commenters posted some code to force the resolution. I've filled in the blanks in my unit test base class:

public class UnitTestBase
{
    static DataTests()
    {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;
    }

    public static Assembly CurrentDomainAssemblyResolve(
        object sender, ResolveEventArgs args)
    {
        var name = new AssemblyName(args.Name);
        return name.Name == "Ninject" 
            ? typeof(KernelBase).Assembly : null;
    }
}
Thesis answered 22/10, 2012 at 13:21 Comment(0)
S
6

I don't have a specific answer, but when tracking down this sort of issue I use the Fusion log viewer. Open up a Visual Studio Command Prompt and run fuslogvw.exe

If you've not done this before the logging will be disabled, go into the settings and set it to Log binding failures to disk. If you find it doesn't initially show any logs, try starting the command prompt in Admin mode.

I've had issues like this recently, and it was this tool that worked it out for me, though interestingly it usually always works in Resharper.

The solution is almost always a Binding problem, so you're in the right place.

Springlet answered 15/10, 2012 at 13:43 Comment(4)
Done that, but nothing appeared in the log viewer.Thesis
It is quite a temperamental beast, when you are viewing the logs don't run it as an Admin, the logs are stored somewhere in your local user folder, so if the Admin user is not your usual login, it may not be showing the logs because of that.Springlet
Ah right, thanks. Posted the log in the question. Not quite sure what to do about it though.Thesis
Wish I could +2 for actually suggesting fuslogvw.exe - a tool that I have rarely seen utilized quickly enough for stubborn assembly-load errors. Great suggestion.Conney
R
3

There appears to be a bug in VS2012 that doesn't pick up the binding redirects unless you are using a testsettings file http://youtrack.jetbrains.com/issue/RSRP-329746

Regardless which test framework you are using, simply right click the solution and add a new testsettings file. By default, this is empty, which is what you'll need. From there all you need to do is mark it as the active test settings (Tools -> test -> Select active test settings -> name of file)

Update:

As per this link: http://youtrack.jetbrains.com/issue/RSRP-329567 it seems people have been able to get this to workaround to work with R# 7.1

Rubbing answered 16/10, 2012 at 12:9 Comment(6)
I should point out that from a VS perspective, test settings are always added to the solution when you add a mstest project, so it's not really a bug...Rubbing
Thanks for answering Bryan. I've tried this, but there's no change in outcome.Thesis
People have commented in the above link that this works in version 7.1 EAP (October 8th)Rubbing
Just installed Build 7.1.7.40 (16th October). No change.Thesis
I couldn't find (Tools -> test -> Select active test settings -> name of file) in my VS2012 but instead menu:Test -> Test settings -> Select test settings file -> Name of file FWIWToothsome
Also: I had to add a mysolution.vsmdi file to the Solution Items. VS2012 says it is deprecated but I need it non the less. (VS2012, Dotnet4, R#7.1.3)Toothsome
T
2

From this link (mentioned originally by @bryanbcook), although the Resharper 7.1 upgrade didn't work, one of the commenters posted some code to force the resolution. I've filled in the blanks in my unit test base class:

public class UnitTestBase
{
    static DataTests()
    {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;
    }

    public static Assembly CurrentDomainAssemblyResolve(
        object sender, ResolveEventArgs args)
    {
        var name = new AssemblyName(args.Name);
        return name.Name == "Ninject" 
            ? typeof(KernelBase).Assembly : null;
    }
}
Thesis answered 22/10, 2012 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.