5

我有一组单元测试,它们曾经在 NCrunch 和 Resharper 7(在 VS2012 中)测试运行程序中运行得非常好。我通过 NuGet 添加了 SignalR 和 SignalR.Ninject,现在我的单元测试在 NCrunch 中工作,但不再在 Resharper 中运行。

TestFixtureSetUp 失败并出现以下错误:

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

在 App.Config 中,我有

<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>

Web.Config 中的等效项适用于实际的 MVC 项目。

任何想法如何解决它?

更新

我试过打开和关闭影子复制,结果没有区别。

更新

Jim Skim 的回答生成的日志文件的输出:

*** 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.
4

3 回答 3

6

我没有具体的答案,但在追踪此类问题时,我使用了 Fusion 日志查看器。打开 Visual Studio 命令提示符并运行fuslogvw.exe

如果您在禁用日志记录之前尚未执行此操作,请进入设置并将其设置为Log binding failures to disk. 如果您发现它最初没有显示任何日志,请尝试在管理员模式下启动命令提示符。

我最近遇到了这样的问题,正是这个工具为我解决了这个问题,尽管有趣的是它通常总是在 Resharper 中工作。

解决方案几乎总是一个绑定问题,所以你来对地方了。

于 2012-10-15T13:43:47.983 回答
3

VS2012 中似乎存在一个错误,除非您使用的是测试设置文件http://youtrack.jetbrains.com/issue/RSRP-329746 ,否则它不会获取绑定重定向

无论您使用哪种测试框架,只需右键单击解决方案并添加一个新的 testsettings 文件。默认情况下,这是空的,这是您需要的。从那里您需要做的就是将其标记为活动测试设置(工具 -> 测试 -> 选择活动测试设置 -> 文件名)

更新:

根据此链接:http: //youtrack.jetbrains.com/issue/RSRP-329567似乎人们已经能够解决此问题以使用 R# 7.1

于 2012-10-16T12:09:48.637 回答
2

这个链接(最初由@bryanbcook 提到),虽然 Resharper 7.1 升级不起作用,但其中一位评论者发布了一些代码来强制解决。我已经在我的单元测试基类中填写了空白:

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;
    }
}
于 2012-10-22T13:21:56.577 回答