Skip to content

Commit

Permalink
abstract IHostingEnvironment in unit tests (#2430)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra authored Oct 1, 2021
1 parent a350e89 commit f3de5e0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Microsoft.ApplicationInsights.AspNetCore.Tests
{
using System.IO;

using Moq;

using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;

/// <summary>
/// The type <see cref="IHostingEnvironment"/> was marked Obsolete starting in NetCore3.
/// Here I'm abstracting it's use into a helper method to simplify some of the tests.
/// </summary>
public static class EnvironmentHelper
{
#pragma warning disable CS0618 // Type or member is obsolete
/// <summary>
/// Get an instance of <see cref="IHostingEnvironment"/>.
/// <see cref="IHostingEnvironment.EnvironmentName"/> is set to "UnitTest".
/// <see cref="IHostingEnvironment.ContentRootPath"/> is set to <see cref="Directory.GetCurrentDirectory"/>.
/// </summary>
/// <returns></returns>
public static IHostingEnvironment GetIHostingEnvironment()
{
var mockEnvironment = new Mock<IHostingEnvironment>();
mockEnvironment.Setup(x => x.EnvironmentName).Returns("UnitTest");
mockEnvironment.Setup(x => x.ContentRootPath).Returns(Directory.GetCurrentDirectory());
return mockEnvironment.Object;
}
#pragma warning restore CS0618 // Type or member is obsolete
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Extensions.DependencyInjection.Test
using System.IO;

using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.ApplicationInsights.AspNetCore.Tests;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static ServiceCollection CreateServicesAndAddApplicationinsightsTelemetry
public static ServiceCollection GetServiceCollectionWithContextAccessor()
{
var services = new ServiceCollection();
services.AddSingleton<IHostingEnvironment>(new HostingEnvironment() { ContentRootPath = Directory.GetCurrentDirectory() });
services.AddSingleton<IHostingEnvironment>(EnvironmentHelper.GetIHostingEnvironment());
services.AddSingleton<DiagnosticListener>(new DiagnosticListener("TestListener"));
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static ServiceCollection CreateServicesAndAddApplicationinsightsWorker(s
{
IConfigurationRoot config;
var services = new ServiceCollection()
.AddSingleton<IHostingEnvironment>(new HostingEnvironment() { ContentRootPath = Directory.GetCurrentDirectory() })
.AddSingleton<IHostingEnvironment>(EnvironmentHelper.GetIHostingEnvironment())
.AddSingleton<DiagnosticListener>(new DiagnosticListener("TestListener"));

if (jsonPath != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Moq" Version="4.16.1" />
</ItemGroup>

<ItemGroup Condition="'$(IsNetFramework)' == 'True'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ public class AspNetCoreEnvironmentTelemetryInitializerTests
[Fact]
public void InitializeDoesNotThrowIfHostingEnvironmentIsNull()
{
var initializer = new AspNetCoreEnvironmentTelemetryInitializer(null);
var initializer = new AspNetCoreEnvironmentTelemetryInitializer(environment: null);
initializer.Initialize(new RequestTelemetry());
}

[Fact]
public void InitializeDoesNotOverrideExistingProperty()
{
var initializer = new AspNetCoreEnvironmentTelemetryInitializer(new HostingEnvironment() { EnvironmentName = "Production"});
var initializer = new AspNetCoreEnvironmentTelemetryInitializer(environment: EnvironmentHelper.GetIHostingEnvironment());
var telemetry = new RequestTelemetry();
telemetry.Context.GlobalProperties.Add("AspNetCoreEnvironment", "Development");
initializer.Initialize(telemetry);

Assert.Equal("Development", telemetry.Context.GlobalProperties["AspNetCoreEnvironment"]);
Assert.Equal("Production", telemetry.Properties["AspNetCoreEnvironment"]);
Assert.Equal("UnitTest", telemetry.Properties["AspNetCoreEnvironment"]);
}

[Fact]
public void InitializeSetsCurrentEnvironmentNameToProperty()
{
var initializer = new AspNetCoreEnvironmentTelemetryInitializer(new HostingEnvironment() { EnvironmentName = "Production"});
var initializer = new AspNetCoreEnvironmentTelemetryInitializer(environment: EnvironmentHelper.GetIHostingEnvironment());
var telemetry = new RequestTelemetry();
initializer.Initialize(telemetry);

Assert.Equal("Production", telemetry.Properties["AspNetCoreEnvironment"]);
Assert.Equal("UnitTest", telemetry.Properties["AspNetCoreEnvironment"]);
}
}
}

0 comments on commit f3de5e0

Please sign in to comment.