1
0
Форкнуть 0

address code review comments - move creation of Mock into a separate helper class

This commit is contained in:
Sergey Kanzhelev 2015-05-05 10:14:38 -07:00
Родитель 57f1559e13
Коммит 08738969cf
3 изменённых файлов: 28 добавлений и 32 удалений

Просмотреть файл

@ -2,13 +2,12 @@
{ {
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.ApplicationInsights.AspNet.Tests; using Microsoft.ApplicationInsights.AspNet.Tests.Helpers;
using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Xunit; using Xunit;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
public class ExceptionTrackingMiddlewareTest public class ExceptionTrackingMiddlewareTest
{ {
@ -18,7 +17,7 @@
public async Task InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform() public async Task InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform()
{ {
RequestDelegate nextMiddleware = httpContext => { throw new Exception(); }; 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<Exception>(() => middleware.Invoke(null)); await Assert.ThrowsAnyAsync<Exception>(() => middleware.Invoke(null));
@ -29,23 +28,12 @@
public async Task SdkVersionIsPopulatedByMiddleware() public async Task SdkVersionIsPopulatedByMiddleware()
{ {
RequestDelegate nextMiddleware = httpContext => { throw new Exception(); }; 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<Exception>(() => middleware.Invoke(null)); await Assert.ThrowsAnyAsync<Exception>(() => middleware.Invoke(null));
Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion); Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion);
Assert.Contains("aspnetv5", sentTelemetry.Context.GetInternalContext().SdkVersion); Assert.Contains("aspnet5", 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);
} }
} }
} }

Просмотреть файл

@ -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<ITelemetry> onSendCallback)
{
var telemetryChannel = new FakeTelemetryChannel { OnSend = onSendCallback };
var telemetryConfiguration = new TelemetryConfiguration();
telemetryConfiguration.InstrumentationKey = "REQUIRED";
telemetryConfiguration.TelemetryChannel = telemetryChannel;
return new TelemetryClient(telemetryConfiguration);
}
}
}

Просмотреть файл

@ -1,10 +1,9 @@
namespace Microsoft.ApplicationInsights.AspNet namespace Microsoft.ApplicationInsights.AspNet
{ {
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.ApplicationInsights.AspNet.Tests; using Microsoft.ApplicationInsights.AspNet.Tests.Helpers;
using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http.Core; using Microsoft.AspNet.Http.Core;
@ -21,23 +20,12 @@
httpContext.Response.StatusCode = 200; httpContext.Response.StatusCode = 200;
await httpContext.Response.Body.WriteAsync(new byte[0], 0, 0); 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()); await middleware.Invoke(new DefaultHttpContext(), new RequestTelemetry());
Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion); Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion);
Assert.Contains("aspnetv5", sentTelemetry.Context.GetInternalContext().SdkVersion); Assert.Contains("aspnet5", 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);
} }
} }
} }