20

I am currently upgrading a project from .NET Core RC1 to the new RTM 1.0 version. In RC1, there was a IApplicationEnvironment which was replaced with IHostingEnvironment in version 1.0

In RC1 I could do this

public class MyClass
{
    protected static IApplicationEnvironment ApplicationEnvironment { get;private set; }

    public MyClass()
    {
        ApplicationEnvironment = PlatformServices.Default.Application;
    }
}

Does anyone know how to achieve this in v1.0?

public class MyClass
{
    protected static IHostingEnvironment HostingEnvironment { get;private set; }

    public MyClass()
    {
        HostingEnvironment = ???????????;
    }
}
Nkosi
  • 191,971
  • 29
  • 311
  • 378
Bill Posters
  • 759
  • 2
  • 9
  • 16

3 Answers3

23

You can mock the IHostEnvironment using a mocking framework if needed or create a fake version by implementing the interface.

Give a class like this...

public class MyClass {
    protected IHostingEnvironment HostingEnvironment { get;private set; }

    public MyClass(IHostingEnvironment host) {
        HostingEnvironment = host;
    }
}

You can setup a unit test example using Moq...

public void TestMyClass() {
    //Arrange
    var mockEnvironment = new Mock<IHostingEnvironment>();
    //...Setup the mock as needed
    mockEnvironment
        .Setup(m => m.EnvironmentName)
        .Returns("Hosting:UnitTestEnvironment");
    //...other setup for mocked IHostingEnvironment...

    //create your SUT and pass dependencies
    var sut = new MyClass(mockEnvironment.Object);

    //Act
    //...call you SUT

    //Assert
    //...assert expectations
}
Nkosi
  • 191,971
  • 29
  • 311
  • 378
  • In such way you can't setup extension method like IsDevelopment(); – user1785960 Apr 10 '20 at 08:10
  • 3
    @user1785960 Why do you need to setup it? If you setup the "EnvironmentName" of the mock object to "Development" then the 'IsDevelopment' extension method will return 'true'. At least in my case, that's all I need. – Razor23 Donetsk Jun 23 '20 at 19:35
10

Using Microsoft.Extensions.Hosting (which is one of the included packages in ASP.NET Core), you can use:

IHostEnvironment env = 
    new HostingEnvironment { EnvironmentName = Environments.Development };
Shimmy Weitzhandler
  • 92,920
  • 119
  • 388
  • 596
6

In general, as IHostingEnvironment is just an interface, you can simply mock it to return whatever you want.

If you are using TestServer in your tests, the best way to mock is to use WebHostBuilder.Configure method. Something like this:

var testHostingEnvironment = new MockHostingEnvironment(); 
var builder = new WebHostBuilder()
            .Configure(app => { })
            .ConfigureServices(services =>
            {
                services.TryAddSingleton<IHostingEnvironment>(testHostingEnvironment);
            });
var server = new TestServer(builder);
Set
  • 40,147
  • 18
  • 114
  • 133
  • 2
    I don't want to use the TestServer class. That's more for integration testing I believe. I don't need to spin up a complete instance of the app. I just want to test a particular class. What I have is a Test base class that used `ApplicationEnvironment` in RC1, but I cannot seem to replace it easily in 1.0 – Bill Posters Jul 11 '16 at 03:24
  • So why you don't like to just mock it? HostingEnvironment = < your mock implementation of IHostingEnvironment> – Set Jul 11 '16 at 06:22