none
Moles says it "requires tests to be IN an instrumented process." RRS feed

  • Question

  • (This question is also posted at Stack Exchange: http://bit.ly/lr5Bw9 )
    I am using Moles to unit test a small Windows Service.  However, I am encountering a MoleInvalidOperationException that I have not yet seen, nor is there much information available regarding this, on the Web.  Note the upper-case "IN".  I believe I am overlooking something.  See my Stack Exchange post, for additional details. Any help you can offer is welcome.
    OUTPUT WINDOW:
    InitilaizationDetectsMissingMonitorDirectory has failed:
      Test method FtpDirWatcher.Test.FileWatcherTest.InitilaizationDetectsMissingMonitorDirectory threw exception: 
      Microsoft.Moles.Framework.Moles.MoleInvalidOperationException: Moles requires tests to be IN an instrumented process.
      In Visual Studio Unit Test, add the following attribute to your unit test method:
      [TestMethod]
      [HostType("Moles")] // add this attribute
      public void Test() 
      { ... }
      Extensions are also available for most unit test frameworks. Please refer to the Moles manual.
      C:\ ... \FtpDirWatcher.Test\FileWatcherTest.cs(185, 0) : FtpDirWatcher.FileWatcherTest.InitilaizationDetectsMissingMonitorDirectory()
    TEST CODE:
    using System;
    using System.IO;
    using System.Linq;
    using FtpDirWatcher.Moles;
    using Microsoft.Moles.Framework;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace FtpDirWatcher.Test  // Test project namespace
    {
        [TestClass]
        public class FileWatcherTest
        {
            readonly string _invalidDirectory = @"B:\invaliddirectory";
    
            [TestMethod]
            [HostType("Moles")]
            public void InitilaizationDetectsMissingMonitorDirectory()
            {
                Assert.IsFalse(Directory.Exists(_invalidDirectory));
    
                // THE FOLLOWING LINE OF CODE THROWS THE ERROR.
                // Use moles to detour the MonitorDirectory property's Get
                // method to a delegate.
                MFileWatcher.AllInstances.MonitorDirectoryGet = watcher =>
                    new DirectoryInfo(_invalidDirectory);
    
                // Don't use the accessor -- no private fields are accessed.
                var target = new FileWatcher();
                Assert.IsFalse(target.IsConfigurationOk);
            }
        }
    }
    

    ~ Mike Christian
    Tuesday, May 10, 2011 7:40 PM

Answers

  • I have resolved this issue.  It's one of those forehead-slap/face-palm solutions.  I was using the DevExpress (Tools for Visual StudioUnit Test Runner to execute the tests.  Moled tests always throw the "Moles requires tests to be IN an instrumented process" exception.  When I run the same test using the Visual Studio 2010 testing tools (i.e. Test View), the tests work just fine.

    The DevExpress testing tool is obviously missing some compiler commands that the Moles/PEX installer integrated into the Visual Studio 2010 testing tools.

     

    EXAMPLE:

    The following target class should be testable by the test class.  This will work correctly with VS2010 testing tools, but will fail when the test is executed by the DevExpress Unit Testing Runner, or by using the test execution icons that appear in the source code editing window.

     

    TARGET CLASS:

    using System.IO;
    
    namespace InstanceMoleDemo
    {
      public class Class1
      {
        public Class1(IMySettings settings)
        {
          if (!settings.DirectoryInfo1.Exists)
            settings.DirectoryInfo1.Create();
    
          if (!settings.DirectoryInfo2.Exists)
            settings.DirectoryInfo2.Create();
    
          if (!settings.DirectoryInfo3.Exists)
            settings.DirectoryInfo3.Create();
        }
      }
    
      public interface IMySettings
      {
        DirectoryInfo DirectoryInfo1 { get; }
        DirectoryInfo DirectoryInfo2 { get; }
        DirectoryInfo DirectoryInfo3 { get; }
      }
    
      public class MySettings : IMySettings
      {
        public MySettings(string directoryPath1, string directoryPath2, string directoryPath3)
        {
          _directoryInfo1 = new DirectoryInfo(directoryPath1);
          _directoryInfo2 = new DirectoryInfo(directoryPath2);
          _directoryInfo3 = new DirectoryInfo(directoryPath3);
        }
    
        private DirectoryInfo _directoryInfo1;
        public DirectoryInfo DirectoryInfo1
        {
          get { return _directoryInfo1; }
        }
    
        private DirectoryInfo _directoryInfo2;
        public DirectoryInfo DirectoryInfo2
        {
          get { return _directoryInfo2; }
        }
    
        private DirectoryInfo _directoryInfo3;
        public DirectoryInfo DirectoryInfo3
        {
          get { return _directoryInfo3; }
        }
      }
    }
    

     

    TEST CLASS:

    using System.IO;
    using System.IO.Moles;
    using InstanceMoleDemo;
    using Microsoft.Moles.Framework;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    [assembly: MoledType(typeof(System.IO.DirectoryInfo))]
    
    namespace TestProject1
    {
      [TestClass]
      public class UnitTest1
      {
        [TestMethod]
        [HostType("Moles")]
        public void Class1ConstructorTestsDirectory1()
        {
          // Arrange.
          var result = false;
    
          MDirectoryInfo.AllInstances.ExistsGet = dirInfo => true;
          MDirectoryInfo.AllInstances.Create = dirInfo => {};
    
          var settings = new TestMySettings()
          {
            DirectoryInfo1 = new DirectoryInfo("S:\\BogusDirectory"),
            DirectoryInfo2 = new DirectoryInfo("S:\\BogusDirectory"),
            DirectoryInfo3 = new DirectoryInfo("S:\\BogusDirectory")
          };
    
          // Mole the settings.DirectoryInfo3 instance.
          var moledD3 = new MDirectoryInfo(settings.DirectoryInfo3);
          moledD3.ExistsGet = () =>
          {
            result = true;
            return false;
          };
    
          // Act.
          var target = new Class1(settings);
    
          // Assert.
          Assert.IsTrue(result);
        }
      }
    
      public class TestMySettings : IMySettings
      {
        public DirectoryInfo DirectoryInfo1 { get; set; }
        public DirectoryInfo DirectoryInfo2 { get; set; }
        public DirectoryInfo DirectoryInfo3 { get; set; }
      }
    }
    

     

     


    ~ Mike Christian
    Tuesday, May 31, 2011 9:17 PM

All replies

  • This error message occurs when you are trying to launch Moles without the [HostType("Moles")] attribute. However, I see that you actually have the attribute on the method so the issue looks suspicious. Are you using the built-in MSTest runner from VisualStudio? Are you executing as a x64 process?

     


    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Tuesday, May 10, 2011 8:14 PM
  • I am indeed using the built-in MSTest runner from VS2010.  I have VS2010 SP1 installed, if that is of any concern.

    I already checked to ensure I am not running in x64.  I even cranked everything down to x86, set the configuration to build every assembly, and added the assembly setting for bitness, for good measure:

    [assemblyMolesAssemblySettings(Bitness = MolesBitness.x86)]
    

    This is my rebuild output (file paths truncated):

     ------ Rebuild All started: Project: Common, Configuration: Debug x86 ------
     Common -> C:\...\Common\bin\x86\Debug\Common.dll
    ------ Rebuild All started: Project: FtpDirWatcher, Configuration: Debug x86 ------
     FtpDirWatcher -> C:\...\FtpDirWatcher\bin\Debug\FtpDirWatcher.exe
    ------ Rebuild All started: Project: FtpDirWatcher.Test, Configuration: Debug x86 ------
     Microsoft Moles v0.94.51023.0 - http://research.microsoft.com/moles - .NET v4.0.30319
     Copyright (c) Microsoft Corporation 2007-2010. All rights reserved.
     
     00:00:00.00> moles
     Moles : info : metadata : ignoring reference C:\...\FtpDirWatcher.Test\MolesAssemblies\FtpDirWatcher.Moles.dll
     Moles : info : metadata : incompatible assembly bitness, using reflection only
     Moles : info : metadata : loading C:\...\FtpDirWatcher\bin\Debug\FtpDirWatcher.exe (reflection only)
     Moles : info : compilation : output assembly name: FtpDirWatcher.Moles
     Moles : info : code : found 4 types
     Moles : info : code : visibility: exported or assembly(FtpDirWatcher.Moles)
     00:00:00.37> code generation
      Moles : info : code : generating code at C:\...\FtpDirWatcher.Test\obj\x86\Debug\Moles\befw\m.g.cs
      00:00:00.52> stubs generation
      Moles : info : code : generated 2 stub types
      00:00:00.89> moles generation
      Moles : info : code : generated 2 mole types
     00:00:01.45> compiling
      Moles : info : compilation : Moles assembly: C:\...\FtpDirWatcher.Test\MolesAssemblies\FtpDirWatcher.Moles.dll
     00:00:02.37> moles generator 0 errors, 0 warnings
     FtpDirWatcher.Test -> C:\...\FtpDirWatcher.Test\bin\x86\Debug\FtpDirWatcher.Test.dll
    ========== Rebuild All: 3 succeeded, 0 failed, 0 skipped ==========

    ~ Mike Christian
    Tuesday, May 10, 2011 8:21 PM
  • I have resolved this issue.  It's one of those forehead-slap/face-palm solutions.  I was using the DevExpress (Tools for Visual StudioUnit Test Runner to execute the tests.  Moled tests always throw the "Moles requires tests to be IN an instrumented process" exception.  When I run the same test using the Visual Studio 2010 testing tools (i.e. Test View), the tests work just fine.

    The DevExpress testing tool is obviously missing some compiler commands that the Moles/PEX installer integrated into the Visual Studio 2010 testing tools.

     

    EXAMPLE:

    The following target class should be testable by the test class.  This will work correctly with VS2010 testing tools, but will fail when the test is executed by the DevExpress Unit Testing Runner, or by using the test execution icons that appear in the source code editing window.

     

    TARGET CLASS:

    using System.IO;
    
    namespace InstanceMoleDemo
    {
      public class Class1
      {
        public Class1(IMySettings settings)
        {
          if (!settings.DirectoryInfo1.Exists)
            settings.DirectoryInfo1.Create();
    
          if (!settings.DirectoryInfo2.Exists)
            settings.DirectoryInfo2.Create();
    
          if (!settings.DirectoryInfo3.Exists)
            settings.DirectoryInfo3.Create();
        }
      }
    
      public interface IMySettings
      {
        DirectoryInfo DirectoryInfo1 { get; }
        DirectoryInfo DirectoryInfo2 { get; }
        DirectoryInfo DirectoryInfo3 { get; }
      }
    
      public class MySettings : IMySettings
      {
        public MySettings(string directoryPath1, string directoryPath2, string directoryPath3)
        {
          _directoryInfo1 = new DirectoryInfo(directoryPath1);
          _directoryInfo2 = new DirectoryInfo(directoryPath2);
          _directoryInfo3 = new DirectoryInfo(directoryPath3);
        }
    
        private DirectoryInfo _directoryInfo1;
        public DirectoryInfo DirectoryInfo1
        {
          get { return _directoryInfo1; }
        }
    
        private DirectoryInfo _directoryInfo2;
        public DirectoryInfo DirectoryInfo2
        {
          get { return _directoryInfo2; }
        }
    
        private DirectoryInfo _directoryInfo3;
        public DirectoryInfo DirectoryInfo3
        {
          get { return _directoryInfo3; }
        }
      }
    }
    

     

    TEST CLASS:

    using System.IO;
    using System.IO.Moles;
    using InstanceMoleDemo;
    using Microsoft.Moles.Framework;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    [assembly: MoledType(typeof(System.IO.DirectoryInfo))]
    
    namespace TestProject1
    {
      [TestClass]
      public class UnitTest1
      {
        [TestMethod]
        [HostType("Moles")]
        public void Class1ConstructorTestsDirectory1()
        {
          // Arrange.
          var result = false;
    
          MDirectoryInfo.AllInstances.ExistsGet = dirInfo => true;
          MDirectoryInfo.AllInstances.Create = dirInfo => {};
    
          var settings = new TestMySettings()
          {
            DirectoryInfo1 = new DirectoryInfo("S:\\BogusDirectory"),
            DirectoryInfo2 = new DirectoryInfo("S:\\BogusDirectory"),
            DirectoryInfo3 = new DirectoryInfo("S:\\BogusDirectory")
          };
    
          // Mole the settings.DirectoryInfo3 instance.
          var moledD3 = new MDirectoryInfo(settings.DirectoryInfo3);
          moledD3.ExistsGet = () =>
          {
            result = true;
            return false;
          };
    
          // Act.
          var target = new Class1(settings);
    
          // Assert.
          Assert.IsTrue(result);
        }
      }
    
      public class TestMySettings : IMySettings
      {
        public DirectoryInfo DirectoryInfo1 { get; set; }
        public DirectoryInfo DirectoryInfo2 { get; set; }
        public DirectoryInfo DirectoryInfo3 { get; set; }
      }
    }
    

     

     


    ~ Mike Christian
    Tuesday, May 31, 2011 9:17 PM