在XUnit中使用Moles - 错误的dll版本

时间:2011-05-11 23:06:34

标签: .net dll xunit moles pex-and-moles

我正在尝试设置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?

3 个答案:

答案 0 :(得分:2)

您无法在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>

答案 1 :(得分: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 的任何更改,这是唯一的依赖性。

答案 2 :(得分:2)

尽管proxon的回答对于完成任务非常有帮助,但是当我进一步调查时,请允许我提供一个更好的答案(希望能帮助其他人解决这个问题)。源代码可在C:\Program Files\Microsoft Moles\Documentation\moles.samples.zip中找到。当然,与代理反编译的代码几乎完全相同。

你也可以在那里找到NUnit和MbUnit包装器。