diff --git a/test/Microsoft.ApplicationInsights.AspNet.Tests/ExceptionTrackingMiddlewareTest.cs b/test/Microsoft.ApplicationInsights.AspNet.Tests/ExceptionTrackingMiddlewareTest.cs index 9fe07df..128b8e1 100644 --- a/test/Microsoft.ApplicationInsights.AspNet.Tests/ExceptionTrackingMiddlewareTest.cs +++ b/test/Microsoft.ApplicationInsights.AspNet.Tests/ExceptionTrackingMiddlewareTest.cs @@ -2,13 +2,12 @@ { using System; using System.Threading.Tasks; - using Microsoft.ApplicationInsights.AspNet.Tests; + using Microsoft.ApplicationInsights.AspNet.Tests.Helpers; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts; - using Microsoft.ApplicationInsights.Extensibility; + using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.AspNet.Builder; using Xunit; - using Microsoft.ApplicationInsights.Extensibility.Implementation; public class ExceptionTrackingMiddlewareTest { @@ -18,7 +17,7 @@ public async Task InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform() { RequestDelegate nextMiddleware = httpContext => { throw new Exception(); }; - var middleware = new ExceptionTrackingMiddleware(nextMiddleware, MockTelemetryClient()); + var middleware = new ExceptionTrackingMiddleware(nextMiddleware, CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry)); await Assert.ThrowsAnyAsync(() => middleware.Invoke(null)); @@ -29,23 +28,12 @@ public async Task SdkVersionIsPopulatedByMiddleware() { RequestDelegate nextMiddleware = httpContext => { throw new Exception(); }; - var middleware = new ExceptionTrackingMiddleware(nextMiddleware, MockTelemetryClient()); + var middleware = new ExceptionTrackingMiddleware(nextMiddleware, CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry)); await Assert.ThrowsAnyAsync(() => middleware.Invoke(null)); Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion); - Assert.Contains("aspnetv5", sentTelemetry.Context.GetInternalContext().SdkVersion); - } - - private TelemetryClient MockTelemetryClient() - { - var telemetryChannel = new FakeTelemetryChannel { OnSend = telemetry => this.sentTelemetry = telemetry }; - - var telemetryConfiguration = new TelemetryConfiguration(); - telemetryConfiguration.InstrumentationKey = "REQUIRED"; - telemetryConfiguration.TelemetryChannel = telemetryChannel; - - return new TelemetryClient(telemetryConfiguration); + Assert.Contains("aspnet5", sentTelemetry.Context.GetInternalContext().SdkVersion); } } } diff --git a/test/Microsoft.ApplicationInsights.AspNet.Tests/Helpers/CommonMocks.cs b/test/Microsoft.ApplicationInsights.AspNet.Tests/Helpers/CommonMocks.cs new file mode 100644 index 0000000..02ced94 --- /dev/null +++ b/test/Microsoft.ApplicationInsights.AspNet.Tests/Helpers/CommonMocks.cs @@ -0,0 +1,20 @@ +namespace Microsoft.ApplicationInsights.AspNet.Tests.Helpers +{ + using System; + using Microsoft.ApplicationInsights.Channel; + using Microsoft.ApplicationInsights.Extensibility; + + public static class CommonMocks + { + public static TelemetryClient MockTelemetryClient(Action onSendCallback) + { + var telemetryChannel = new FakeTelemetryChannel { OnSend = onSendCallback }; + + var telemetryConfiguration = new TelemetryConfiguration(); + telemetryConfiguration.InstrumentationKey = "REQUIRED"; + telemetryConfiguration.TelemetryChannel = telemetryChannel; + + return new TelemetryClient(telemetryConfiguration); + } + } +} diff --git a/test/Microsoft.ApplicationInsights.AspNet.Tests/RequestTrackingMiddlewareTest.cs b/test/Microsoft.ApplicationInsights.AspNet.Tests/RequestTrackingMiddlewareTest.cs index a1779ef..1949d88 100644 --- a/test/Microsoft.ApplicationInsights.AspNet.Tests/RequestTrackingMiddlewareTest.cs +++ b/test/Microsoft.ApplicationInsights.AspNet.Tests/RequestTrackingMiddlewareTest.cs @@ -1,10 +1,9 @@ namespace Microsoft.ApplicationInsights.AspNet { using System.Threading.Tasks; - using Microsoft.ApplicationInsights.AspNet.Tests; + using Microsoft.ApplicationInsights.AspNet.Tests.Helpers; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts; - using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http.Core; @@ -21,23 +20,12 @@ httpContext.Response.StatusCode = 200; await httpContext.Response.Body.WriteAsync(new byte[0], 0, 0); }; - var middleware = new RequestTrackingMiddleware(nextMiddleware, MockTelemetryClient()); + var middleware = new RequestTrackingMiddleware(nextMiddleware, CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry)); await middleware.Invoke(new DefaultHttpContext(), new RequestTelemetry()); Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion); - Assert.Contains("aspnetv5", sentTelemetry.Context.GetInternalContext().SdkVersion); - } - - private TelemetryClient MockTelemetryClient() - { - var telemetryChannel = new FakeTelemetryChannel { OnSend = telemetry => this.sentTelemetry = telemetry }; - - var telemetryConfiguration = new TelemetryConfiguration(); - telemetryConfiguration.InstrumentationKey = "REQUIRED"; - telemetryConfiguration.TelemetryChannel = telemetryChannel; - - return new TelemetryClient(telemetryConfiguration); + Assert.Contains("aspnet5", sentTelemetry.Context.GetInternalContext().SdkVersion); } } }