address code review comments - move creation of Mock into a separate helper class
This commit is contained in:
Родитель
57f1559e13
Коммит
08738969cf
|
@ -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<Exception>(() => 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<Exception>(() => 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче