2014-10-07 6 views
0

Visual Studio Ultimate 2010에서 Microsoft Moles를 사용 중이며 Directory.Exists 메서드를 두드려 고합니다. 내 어셈블리 및 다음 헤더 줄에 mscorlib.moles 파일이 있지만 여전히 단위 테스트를 실행하려고하면 MoleNotInstrumentedException이 발생합니다. 필자는 전에 Microsoft Fakes를 사용해 봤지만 내가하고있는 프로젝트로 VS2010을 사용하게되므로 Moles를 사용해야합니다. local.testsettings 파일의 호스트 유형을 Moles로 변경했습니다. 누구나 단위 테스트에서 오류가 발생하거나 문제가 발생하는 이유를 알고 있습니까?Microsoft Moles Directory Exists throw MsNotInstrumentedException

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 

답변

1

이 문제는

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

의 선이 공간 안에이었다이었다. 이로 인해 컴파일러는 라인을 볼 수 없어 MoleNotInstrumentedException을 던졌습니다. 해결책은 네임 스페이스 외부로이 줄을 이동하는 것이 었습니다. 일단이 작업을 수행하면 Moles 호스트를 사용하는 모든 단위 테스트가 올바르게 작동하기 시작했습니다.