【问题标题】:Microsoft Moles Directory Exists throws MoleNotInstrumentedExceptionMicrosoft Moles 目录存在引发 MoleNotInstrumentedException
【发布时间】:2014-10-07 17:38:04
【问题描述】:

我在 Visual Studio Ultimate 2010 中使用 Microsoft Moles 并尝试使用 Directory.Exists 方法。我的程序集中有 mscorlib.moles 文件和以下标题行,但在尝试运行单元测试时仍然收到 MoleNotInstrumentedException。我以前使用过 Microsoft Fakes,但我正在工作的项目使我们使用 VS2010,所以我必须使用 Moles。我已将 local.testsettings 文件中的主机类型更改为 Moles。任何人都知道为什么单元测试可能会抛出错误或出现问题?

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))]

正在测试失败的简化方法。

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

测试方法。

[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");
    }
}

异常错误信息

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))]

异常堆栈跟踪

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

【问题讨论】:

    标签: c# .net visual-studio-2010 unit-testing moles


    【解决方案1】:

    问题在于

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

    在命名空间内。这导致编译器无法看到该行,因此抛出 MoleNotInstrumentedException。解决方案是将这条线移到命名空间之外。一旦我这样做了,所有使用 Moles 主机的单元测试都开始正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2012-07-23
      • 1970-01-01
      相关资源
      最近更新 更多