title: Mocking an implementation class with test class within unit test in C#
body: <p>I'm trying to write a simple unit test where I have wrapped the HttpContext.Current.Server.MapPath with an interface and implementation. I'm not positive if the implementation is in the right place.</p>
  
  <pre><code>public class FooGenerator
  {
      ServerPathProvider serverPathProvider = new ServerPathProvider();
  
      public string generateFoo()
      {
          BarWebService bws = new BarWebService(serverPathProvider.MapPath("~/path/file"));
          return stuff;
      }
  }
  
  
  public class ServerPathProvider : IPathProvider
  {
      public string MapPath(string path)
      {
          return HttpContext.Current.Server.MapPath(path);
      }
  }
  </code></pre>
  
  <p>In the test I have my own implementation I want to use, but I cannot figure out how to inject this into the real class during the unit test.  </p>
  
  <pre><code>[TestClass()]
  public class FooGeneratorTests
  {
      [TestMethod()]
      public void generateFooTest()
      {
          FooGenerator fg = new FooGenerator();
  
          //Some kind of mock dependency injection
          Mock&lt;IPathProvider&gt; provider = new Mock&lt;IPathProvider&gt;();
          //stub ServerPathProvider object with provider mock
  
          string token = fg.generateFoo;
          Assert.IsNotNull(token);
      }
  }
  
  public class TestPathProvider : IPathProvider
  {
      public string MapPath(string path)
      {
          return Path.Combine(@"C:\project\", path);
      }
  }
  </code></pre>
  
  <p>Finally, here is my interface just in case.  Basically I just want to swap out two implementations depending on whether or not I am unit test.  This is my first unit test so much of this is new to me. Sorry if I'm missing something basic, but I've been digging through stack overflow for a while now and can't find the steps to do this part.  </p>
  
  <pre><code>public interface IPathProvider
  {
      string MapPath(string path);
  }
  </code></pre>