Skip to content

Commit

Permalink
Split unit test for code first iKey into two tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnduffy committed Nov 30, 2016
1 parent 1b5afdc commit d190e23
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Test/CoreSDK.Test/Shared/TelemetryClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public void ChannelIsInitializedInTrackWhenTelemetryConfigurationIsConstructedVi
}

[TestMethod]
public void TrackUsesInstrumentationKeyIfSetInCodeFirst()
public void TrackUsesInstrumentationKeyFromClientContextIfSetInCodeFirst()
{
ClearActiveTelemetryConfiguration();
PlatformSingleton.Current = new StubPlatform();
Expand All @@ -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);
Expand Down

0 comments on commit d190e23

Please sign in to comment.