I have to unittest one method that uses appSettings to get filename
, that is later used inside HostingEnvironment.MapPath
.
BLL method to test contains the following line where _appSettings.SampleName return some filename in Web project. Later filePath is used to open given file.
string filePath = HostingEnvironment.MapPath(_appSettings.SampleName);
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
...
In UnitTesting project I have the following structure:
<UnitTestProject.Test>\Resources\MyFile.xlsx
In my unittest I was able to mock _appSettings (_appSettings: IAppSettings) and I can set _appSettings.SampleName any value I wish. This value is really taken inside method I am testing. However, I tried to set _appSettings.SampleName to "Resources\MyFile.xlsx" as well as other values like @"....\Resources\MyFile.xlsx".
However, I always get filePath = null
.
Someone marked that htis is duplicate: Unit Testing: DateTime.Now, but I do not need to Mock this method. I have already mocked appsettings, but I am not able to understand why HostingEnvironment.MapPath doesn't work as I want. I am mocking appsettings returing path I wish, but HostingEnvironmentm.MapPath doesn't work. THus probably I do not understand how HostingEnvironment works.