Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.1479 (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?

like image 270
Smashery Avatar asked May 11 '11 23:05

Smashery


2 Answers

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>
like image 125
mthierba Avatar answered Sep 30 '22 14:09

mthierba


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.

like image 23
StanislawSwierc Avatar answered Sep 30 '22 13:09

StanislawSwierc