Search code examples
c#.netvisual-studio-2010unit-testingmoles

Microsoft Moles Directory Exists throws MoleNotInstrumentedException


I'm using Microsoft Moles in Visual Studio Ultimate 2010 and trying to mole the Directory.Exists method. I have the mscorlib.moles file in my assembly and the following header lines but I still get a MoleNotInstrumentedException when trying to run the unit test. I've used Microsoft Fakes before but the project I'm working makes us use VS2010 so I have to use Moles. I have changed the host type in the local.testsettings file to Moles. Anyone have an idea why the unit test may be throwing the error or having a problem?

using System.IO;
using System.IO.Moles;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: MoledAssembly("System.IO")]
[assembly: MoledType(typeof(System.IO.Directory))]

Simplified method being tested that fails.

private bool GetDirectoryExists(string directoryPath)
{
    return Directory.Exists(directoryPath);
}

Test Method.

[TestMethod, TestCategory("Developer"), HostType("Moles")]
public void Test_MolesDirectoryExists()
{
    string shouldBeValue = @"C:\Hello\Nope\Not Here";
    string returnedValue = null;

    using (MolesContext.Create())
    {
        MDirectory.ExistsString = s =>
        {
            Trace.WriteLine("Passed in value: " + s);
            returnedValue = s;
            return true;
        };

        bool result = this.GetDirectoryExists(shouldBeValue);

        Trace.WriteLine("DirectoryExists: " + result);
        Assert.IsTrue(result, "Directory does not exist.");

        Assert.AreEqual(shouldBeValue, returnedValue, "Path values do not match");
    }
}

Exception Error Message

Test method TempUsersService.Tests.TempUsersTests.Test_ShimDirectoryExists threw exception: 
Microsoft.Moles.Framework.Moles.MoleNotInstrumentedException: The System.Boolean
System.IO.Directory.Exists(System.String path) was not instrumented
To resolve this issue, add the following attribute in the test project:

using Microsoft.Moles.Framework;
[assembly: MoledType(typeof(System.IO.Directory))]

Exception Stack Trace

Microsoft.ExtendedReflection.Monitoring._Detours.InvokeEvent[T](T value, SafeAction`1 eh)
Microsoft.ExtendedReflection.Monitoring._Detours.OnAttachedUninstrumentedMethod(Method method)
Microsoft.ExtendedReflection.Monitoring._Detours.CheckInstrumentation(Method method)
Microsoft.ExtendedReflection.Monitoring._Detours.AttachDetour(Object _receiver, Method method, Delegate detourDelegate)
Microsoft.Moles.Framework.Moles.MoleRuntime.SetMoleMethod(Delegate _stub, Object _receiver, Method method)
Microsoft.Moles.Framework.Moles.MoleRuntime.SetMole(Delegate _stub, Type receiverType, Object _receiver, String name, MoleBindingFlags flags, Type[] parameterTypes)
Microsoft.Moles.Framework.Moles.MoleRuntime.SetMolePublicStatic(Delegate _stub, Type receiverType, String name, Type[] parameterTypes)
System.IO.Moles.MDirectory.set_ExistsString(Func`2 value) in c:\Users\abc123\Desktop\workspace\Project\Tests\TempUsersService.Tests\obj\x86\Debug\Moles\m\m.g.cs: line 0
TempUsersService.Tests.TempUsersTests.Test_ShimDirectoryExists() in C:\Users\abc123\Desktop\workspace\Project\Tests\TempUsersService.Tests\TempUsersTests.cs: line 401

Solution

  • The problem was that the line for

    [assembly: MoledType(typeof(System.IO.Directory))] 
    

    was inside the namespace. This caused the compiler to not be able to see the line therefore throwing the MoleNotInstrumentedException. The solution was to move this line outside the namespace. Once I did this all the unit tests that used the Moles host started working correctly.