2014-10-07 46 views
0

我在Visual Studio Ultimate 2010中使用Microsoft Moles並試圖詛咒Directory.Exists方法。我在我的程序集和下面的標題行中有mscorlib.moles文件,但在嘗試運行單元測試時仍然遇到MoleNotInstrumentedException。我之前使用過微軟假貨,但我正在使用的項目讓我們使用VS2010,所以我必須使用Moles。我已將local.testsettings文件中的主機類型更改爲Moles。任何人都有一個想法,爲什麼單元測試可能會拋出錯誤或有問題?Microsoft Moles目錄存在拋出MoleNotInstrumentedException

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主機的單元測試開始正常工作。