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