【问题标题】:Using Moles with XUnit - wrong dll version将 Moles 与 XUnit 一起使用 - 错误的 dll 版本
【发布时间】:2011-08-23 17:11:11
【问题描述】:
我正在尝试设置 Moles 以在我们的单元测试中使用。我们正在使用 xunit,所以我是 using 与 moles (Microsoft.Moles.Framework.Xunit) 一起提供的 Xunit 扩展。但是,当我们运行 Xunit 1.7 时,Moles 抱怨我没有运行版本 1.6.1.1521(带有FileLoadException)。
Moles Manual(第 28 页)确实说:
xUnit.net 版本:
1.5.0.1479(对于其他xUnit.net版本,从源重新编译属性)
这就是我卡住的地方 - 这个 xunit 扩展的源代码在某处可用吗?还是我必须使用 Moles 需要的特定版本的 xunit?
【问题讨论】:
标签:
.net
dll
xunit
moles
pex-and-moles
【解决方案1】:
您不能在moles.runner.exe.config 中定义程序集绑定重定向吗?
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity
name="xunit.dll"
publicKeyToken="8d05b1bb7a6fdb6c" />
<bindingRedirect oldVersion="1.5.0.1479" newVersion="1.6.1.1521" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
【解决方案2】:
如果您必须重新编译,那么您可以这样做。我查找了 Moles 源代码,但在任何地方都找不到。然后我尝试反汇编Microsoft.Moles.Xunit.dll,我意识到该属性只有几行长。
MoledAttribute 源码:
using System;
using System.Reflection;
using XUnit;
namespace Microsoft.Moles.Framework.Xunit
{
public sealed class MoledAttribute : BeforeAfterTestAttribute
{
// Fields
private IDisposable _molesContext;
public override void Before(MethodInfo methodUnderTest)
{
this._molesContext = MolesContext.Create();
}
public override void After(MethodInfo methodUnderTest)
{
IDisposable disposable = this._molesContext;
if (disposable != null)
{
disposable.Dispose();
}
this._molesContext = null;
}
}
}
您应该创建一个新的类库并添加对您想要的任何版本的 xunit.dll 的引用。它甚至应该适用于 1.8.0.1545,因为我没有注意到对 XUnit.BeforeAfterTestAttribute 的任何更改,这是唯一的依赖。
【解决方案3】:
虽然 proxon 的回答对完成我的任务非常有帮助,但请允许我提出一个我在进一步调查时发现的更好的答案(希望能帮助其他遇到这个问题的人)。源代码位于C:\Program Files\Microsoft Moles\Documentation\moles.samples.zip。当然,与 proxon 反编译的代码几乎相同。
您还可以在其中找到 NUnit 和 MbUnit 包装器。