Using Moles with XUnit - wrong dll version
I'm trying to set up Moles to use in our unit testing. We are using xunit, so I am using
the Xunit extension that comes with moles (Microsoft.Moles.Framework.Xunit
). However, as we are running Xunit 1.7, Moles is complaining that I am not running Version 1.6.1.1521 (with a FileLoadException
).
The Moles Manual (page 28) does say:
xUnit.net Version:
1.5.0.1开发者_开发问答479 (for other xUnit.net versions, recompile the attribute from sources)
This is where I get stuck - is the source code for this xunit extension available somewhere? Or will I have to use the specific version of xunit that Moles requires?
Can't you define a assembly binding redirect in the 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>
If you have to recompile then you can do it. I looked for Moles source code but I couldn't find it anywhere. Then I tried to disassemble Microsoft.Moles.Xunit.dll and I realized that the attribute is just few lines long.
MoledAttribute source code:
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;
}
}
}
You should create a new class library and add reference to your xunit.dll of any version you want. It should even work with 1.8.0.1545 as I haven't noticed any changes to XUnit.BeforeAfterTestAttribute which is the only depencency.
While proxon's answer was very helpful for getting my task done, allow me to present a better answer I found as I investigated further (to hopefully help others coming across this problem). The source code is found in C:\Program Files\Microsoft Moles\Documentation\moles.samples.zip
. Pretty much identical to the code that proxon decompiled, of course.
You can also find the NUnit and MbUnit wrappers in there.
精彩评论