In general, since IHostingEnvironment is just an interface, you can simply mock it to bring back whatever you want.
If you use TestServer in your tests, the best way to mock is to use the WebHostBuilder.Configure method. Something like that:
var testHostingEnvironment = new MockHostingEnvironment(); var builder = new WebHostBuilder() .Configure(app => { }) .ConfigureServices(services => { services.TryAddSingleton<IHostingEnvironment>(testHostingEnvironment); }); var server = new TestServer(builder);
source share