Question

Actually im getting error when the below code is executed from unit test project... where import is an xml file in the below code..

XmlTextReader importReader= new XmlTextReader(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import"));

the above code is present in one of the methods in different project(class library) which is used in test method in the unit test project

where as the above path is present in web project. and the value of the

System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import")

is null..ie unable to get the complete path of the file i guess bcos the hostingenvironment is not running(web project) The above code is working fine when running from the webproject ie it returns the exact path. whereas returning null value when running from test project.

please let me know how to fetch the file path(where file is present in web project) when running from test project.

No correct solution

OTHER TIPS

You could pass the filename as argument to the method instead of having the method attempting to retrieve it:

public void SomeMethod(string filename)
{
    using (XmlTextReader importReader = new XmlTextReader(filename))
    {
        ...
    }
}

Now when calling this method from your web tier (a controller) you could pass the filename like this:

public ActionResult Index()
{
    string filename = Server.MapPath("~/Content/Importcontent/import");
    SomeMethod(filename);
    ...
}

and when inside the test project you could pass some other file that you have deployed with your unit test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top