Skip to content

Commit

Permalink
Add activity unit tests for TelemetryActivity.
Browse files Browse the repository at this point in the history
  • Loading branch information
srdjanjovcic committed May 20, 2020
1 parent 95ad7e1 commit 8ab0002
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ namespace NuGet.Common.Test
{
public class TelemetryActivityTests
{
private readonly Mock<INuGetTelemetryService> _telemetryService = new Mock<INuGetTelemetryService>();
private readonly Mock<INuGetTelemetryService> _telemetryService = new Mock<INuGetTelemetryService>(MockBehavior.Strict);
private TelemetryEvent _telemetryEvent;

private readonly Mock<IDisposable> _activity = new Mock<IDisposable>(MockBehavior.Strict);
private bool _activityDisposed;
private string _activityName;

public TelemetryActivityTests()
{
_telemetryService.Setup(x => x.EmitTelemetryEvent(It.IsAny<TelemetryEvent>()))
.Callback<TelemetryEvent>(x => _telemetryEvent = x);

_telemetryService.Setup(x => x.StartActivity(It.IsAny<string>()))
.Callback<string>(x => _activityName = x)
.Returns(_activity.Object);

_activity.Setup(x => x.Dispose())
.Callback(() => _activityDisposed = true);

TelemetryActivity.NuGetTelemetryService = _telemetryService.Object;
}

Expand Down Expand Up @@ -125,6 +136,26 @@ public void Dispose_Always_EmitsDuration()
Assert.InRange(duration, 0d, 10d);
}

[Fact]
public void Create_will_start_activity()
{
using (var telemetry = TelemetryActivity.Create(CreateNewTelemetryEvent()))
{
}

Assert.Equal("testEvent", _activityName);
}

[Fact]
public void Dispose_will_dispose_activity()
{
using (var telemetry = TelemetryActivity.Create(CreateNewTelemetryEvent()))
{
}

Assert.True(_activityDisposed);
}

private static TelemetryEvent CreateNewTelemetryEvent()
{
return new TelemetryEvent("testEvent", new Dictionary<string, object>());
Expand Down

0 comments on commit 8ab0002

Please sign in to comment.