title: Moles does not work in static constructors
body: <p>I have been having a problem with Mole types not working in static constructors. I have created two simple examples to explain the problem:</p>
  
  <p>I have a simple instance class as follows:</p>
  
  <pre><code>public class InstanceTestReader
  {
      public InstanceTestReader ()
      {
          IFileSystem fileSystem = new FileSystem();
  
          this.Content = fileSystem.ReadAllText("test.txt");
      }
  
      public string Content { get; private set; }
  }
  </code></pre>
  
  <p>I have a unit test for this as follows:</p>
  
  <pre><code>    [TestMethod]
      [HostType("Moles")]
      public void CheckValidFileInstance_WithMoles()
      {
          // Arrange
          string expectedFileName = "test.txt";
          string content = "test text content";
  
          Implementation.Moles.MFileSystem.AllInstances.ReadAllTextString = (moledTarget, suppliedFilename) =&gt;
          {
              Assert.AreEqual(suppliedFilename, expectedFileName, "The filename was incorrect");
              return content;
          };
  
          // Act
          string result = new InstanceTestReader().Content;
  
          // Assert
          Assert.AreEqual(content, result, "The result was incorrect");
      }
  </code></pre>
  
  <p>This works with no problems. </p>
  
  <p>If I change my calling class to be static however (NOT the Moled class, but the calling class), Moles no longer works:</p>
  
  <pre><code>public static class StaticTestReader
  {
      static StaticTestReader ()
      {
          IFileSystem fileSystem = new FileSystem();
  
          Content = fileSystem.ReadAllText("test.txt");
      }
  
      public static string Content { get; private set; }
  }
  </code></pre>
  
  <p>And modify my unit test accordingly:</p>
  
  <pre><code>    [TestMethod]
      [HostType("Moles")]
      public void CheckValidFileStatic_WithMoles()
      {
          // Arrange
          string expectedFileName = "test.txt";
          string content = "test text content";
  
          Implementation.Moles.MFileSystem.AllInstances.ReadAllTextString = (moledTarget, suppliedFilename) =&gt;
          {
              Assert.AreEqual(suppliedFilename, expectedFileName, "The filename was incorrect");
              return content;
          };
  
          // Act
          string result = StaticTestReader.Content;
  
          // Assert
          Assert.AreEqual(content, result, "The result was incorrect");
      }
  </code></pre>
  
  <p>... Now Moles no longer works. When running this test, I get the error "Could not find file 'd:\blah\blah\test.txt'". I get this because Moles is no longer in charge of my FileSystem class, so the unit test is calling the original implementation which is looking for a file on the file system.</p>
  
  <p>So, the only change is that the class which the Moled method is called from is now static. I have not changed the Moled class or method, they are still instance types, so I cannot use the Implementation.Moles.SFileSystem
   syntax as this would be for mocking a static class.</p>
  
  <p>Please could someone help to explain how I can get Moles to work within a static method/constructor?</p>
  
  <p>Many thanks!!!</p>