title: mock HttpContext.Current.Server.MapPath using Moq?
body: <p>im unit testing my home controller. This test worked fine until I added a new feature which saves images.</p>
  
  <p>The method that’s causing the issue is this below.</p>
  
  <pre><code>    public static void SaveStarCarCAPImage(int capID)
      {
          byte[] capBinary = Motorpoint2011Data.RetrieveCapImageData(capID);
  
          if (capBinary != null)
          {
              MemoryStream ioStream = new MemoryStream();
              ioStream = new MemoryStream(capBinary);
  
              // save the memory stream as an image
              // Read in the data but do not close, before using the stream.
  
              using (Stream originalBinaryDataStream = ioStream)
              {
                  var path = HttpContext.Current.Server.MapPath("/StarVehiclesImages");
                  path = System.IO.Path.Combine(path, capID + ".jpg");
                  Image image = Image.FromStream(originalBinaryDataStream);
                  Image resize = image.GetThumbnailImage(500, 375, null, new IntPtr());
                  resize.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
              }
          }
      }
  </code></pre>
  
  <p>As the call is coming from a unit test, HttpContext.Current is null and throws an exception. After reading about Moq and some of the tutorials about using Moq with sessions, im sure it can be done.</p>
  
  <p>so far this the unit test code have come up with, but the issue is HTTPContext.Current is always null, and still throws the exception.</p>
  
  <pre><code>    protected ControllerContext CreateStubControllerContext(Controller controller)
      {
          var httpContextStub = new Mock&lt;HttpContextBase&gt;
          {
              DefaultValue = DefaultValue.Mock
          };
  
          return new ControllerContext(httpContextStub.Object, new RouteData(), controller);
      }
  
      [TestMethod]
      public void Index()
      {
          // Arrange
          HomeController controller = new HomeController();            
          controller.SetFakeControllerContext();
  
          var context = controller.HttpContext;
  
          Mock.Get(context).Setup(s =&gt; s.Server.MapPath("/StarVehiclesImages")).Returns("My Path");
  
          // Act
          ViewResult result = controller.Index() as ViewResult;
  
          // Assert
          HomePageModel model = (HomePageModel)result.Model;
          Assert.AreEqual("Welcome to ASP.NET MVC!", model.Message);
          Assert.AreEqual(typeof(List&lt;Vehicle&gt;), model.VehicleMakes.GetType());
          Assert.IsTrue(model.VehicleMakes.Exists(x =&gt; x.Make.Trim().Equals("Ford", StringComparison.OrdinalIgnoreCase)));
      }
  </code></pre>