From d190e23f59d6fbc6c8eff0b08387609be8429fdc Mon Sep 17 00:00:00 2001 From: David Duffy Date: Wed, 30 Nov 2016 10:32:40 -0800 Subject: [PATCH] Split unit test for code first iKey into two tests. --- .../Shared/TelemetryClientTest.cs | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs index 04cebb9f9b..43b15b64d7 100644 --- a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs +++ b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs @@ -496,7 +496,7 @@ public void ChannelIsInitializedInTrackWhenTelemetryConfigurationIsConstructedVi } [TestMethod] - public void TrackUsesInstrumentationKeyIfSetInCodeFirst() + public void TrackUsesInstrumentationKeyFromClientContextIfSetInCodeFirst() { ClearActiveTelemetryConfiguration(); PlatformSingleton.Current = new StubPlatform(); @@ -507,19 +507,36 @@ public void TrackUsesInstrumentationKeyIfSetInCodeFirst() var configuration = new TelemetryConfiguration { TelemetryChannel = channel }; var client = new TelemetryClient(configuration); - string oldActiveKey = TelemetryConfiguration.Active.InstrumentationKey; string environmentKey = Guid.NewGuid().ToString(); string expectedKey = Guid.NewGuid().ToString(); Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", environmentKey); // Set via the environment variable. - // Set in code method one. client.Context.InstrumentationKey = expectedKey; Assert.DoesNotThrow(() => client.TrackTrace(message)); Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey); - // Set in code method two. - sentTelemetry = null; // Make sure we got a new instance. - client.Context.InstrumentationKey = string.Empty; // Clear method one value. + Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null); + + PlatformSingleton.Current = null; + } + + [TestMethod] + public void TrackUsesInstrumentationKeyFromDefaultConfigInstanceIfSetInCodeFirst() + { + ClearActiveTelemetryConfiguration(); + PlatformSingleton.Current = new StubPlatform(); + string message = "Test Message"; + + ITelemetry sentTelemetry = null; + var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; + var configuration = new TelemetryConfiguration { TelemetryChannel = channel }; + var client = new TelemetryClient(configuration); + + string oldActiveKey = TelemetryConfiguration.Active.InstrumentationKey; + string environmentKey = Guid.NewGuid().ToString(); + string expectedKey = Guid.NewGuid().ToString(); + Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", environmentKey); // Set via the environment variable. + TelemetryConfiguration.Active.InstrumentationKey = expectedKey; // Set in code method two, default config. Assert.DoesNotThrow(() => client.TrackTrace(message)); Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey);