Like unit test code that uses HostingEnvironment.MapPath

I have a code that uses HostingEnvironment.MapPath , which I would like to unit test.

How to configure HostingEnvironment so that it returns the path, and not null in my unit test (mstest) project?

+19
unit-testing asp.net-mvc-3
Jan 05 2018-12-12T00:
source share
4 answers

Why do you have code that depends on HostingEnvironment.MapPath in an ASP.NET MVC application, where you have access to objects like HttpServerUtilityBase that allow you to achieve this and that can be easily mocked and tested on a module?

Take an example: a controller action that uses the abstract server class we want to unit test:

 public class HomeController : Controller { public ActionResult Index() { var file = Server.MapPath("~/App_Data/foo.txt"); return View((object)file); } } 

Now there are many ways unit tests can perform this controller action. Personally, I like to use MVcContrib.TestHelper .

But let's see how we can do this using the mocking framework out of the box. I am using Rhino Mocks for this example:

 [TestMethod] public void Index_Action_Should_Calculate_And_Pass_The_Physical_Path_Of_Foo_As_View_Model() { // arrange var sut = new HomeController(); var server = MockRepository.GeneratePartialMock<HttpServerUtilityBase>(); var context = MockRepository.GeneratePartialMock<HttpContextBase>(); context.Expect(x => x.Server).Return(server); var expected = @"c:\work\App_Data\foo.txt"; server.Expect(x => x.MapPath("~/App_Data/foo.txt")).Return(expected); var requestContext = new RequestContext(context, new RouteData()); sut.ControllerContext = new ControllerContext(requestContext, sut); // act var actual = sut.Index(); // assert var viewResult = actual as ViewResult; Assert.AreEqual(viewResult.Model, expected); } 
+15
Jan 05 2018-12-12T00:
source share

It will depend on which fake or insulating structure you use. You might want to look at: a) creating a wrapper type around a static property that can be mocked, or b) using a framework that can mimic static properties - for example, Moles or Typemock Isolator

0
Jan 05 '12 at 10:05
source share

Well, today I am writing a test for code that I do not control, and they used

  private static String GetApplicationPath() { return HostingEnvironment.ApplicationVirtualPath.TrimEnd('/'); } 

so here's a look at C # hacking to set this value

 var path = "/aaaa/bb"; HostingEnvironment hostingEnvironment; if (HostingEnvironment.IsHosted.isFalse()) new HostingEnvironment(); hostingEnvironment = (HostingEnvironment)typeof(HostingEnvironment).fieldValue("_theHostingEnvironment"); var virtualPath = "System.Web".assembly() .type("VirtualPath").ctor(); virtualPath.field("_virtualPath", path); //return virtualPath.prop("VirtualPathString"); //return virtualPath.prop("VirtualPathStringNoTrailingSlash"); hostingEnvironment.field("_appVirtualPath", virtualPath); //hostingEnvironment.field("_appVirtualPath") == virtualPath; return HostingEnvironment.ApplicationVirtualPath == path; //using System.Web.Hosting 
0
Mar 07 '16 at 14:32
source share

Just use this code.

Create a new folder name. Link in the root directory and add the file to this folder.

Use this

 public static XElement GetFile() { HttpContext.Current = new HttpContext(new HttpRequest("", "http://www.google.com", ""), new HttpResponse(new StringWriter())); var doc = new XmlDocument(); var file = HttpContext.Current.Server.MapPath("\\") + "abc.xml"; doc.Load(file); var e = XElement.Load(new XmlNodeReader(doc)); return e; } 
-four
Apr 29 '13 at 11:37
source share



All Articles