2014-10-07 2 views
0

Я использую Microsoft Moles в Visual Studio Ultimate 2010 и пытаюсь развернуть метод Directory.Exists. У меня есть файл mscorlib.moles в моей сборке и следующие строки заголовков, но при попытке запустить модульный тест я все еще получаю исключение MoleNotInstrumentedException. Раньше я использовал Microsoft Fakes, но проект, над которым я работаю, заставляет нас использовать VS2010, поэтому я должен использовать Moles. Я изменил тип хоста в файле local.testsettings на Moles. У кого-нибудь есть идея, почему единичный тест может вызывать ошибку или иметь проблему?Microsoft Moles Directory Exests throws 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, начали работать правильно.

Смежные вопросы