25

I have an MVC web app, and I'm using Simple Injector for DI. Almost all my code is covered by unit tests. However, now that I've added some telemetry calls in some controllers, I'm having trouble setting up the dependencies.

The telemetry calls are for sending metrics to the Microsoft Azure-hosted Application Insights service. The app is not running in Azure, just a server with ISS. The AI portal tells you all kinds of things about your application, including any custom events you send using the telemetry library. As a result, the controller requires an instance of Microsoft.ApplicationInsights.TelemetryClient, which has no Interface and is a sealed class, with 2 constructors. I tried registering it like so (the hybrid lifestyle is unrelated to this question, I just included it for completeness):

// hybrid lifestyle that gives precedence to web api request scope
var requestOrTransientLifestyle = Lifestyle.CreateHybrid(
    () => HttpContext.Current != null,
    new WebRequestLifestyle(),
    Lifestyle.Transient);

container.Register<TelemetryClient>(requestOrTransientLifestyle);

The problem is that since TelemetryClient has 2 constructors, SI complains and fails validation. I found a post showing how to override the container's constructor resolution behavior, but that seems pretty complicated. First I wanted to back up and ask this question:

If I don't make the TelemetryClient an injected dependency (just create a New one in the class), will that telemetry get sent to Azure on every run of the unit test, creating lots of false data? Or is Application Insights smart enough to know it is running in a unit test, and not send the data?

Any "Insights" into this issue would be much appreciated!

Thanks

spottedmahn
  • 11,379
  • 7
  • 75
  • 144
Randy Gamage
  • 1,444
  • 3
  • 18
  • 27
  • 2
    I can't help with the AI side of the question but the registration can be done simply by registering a delegate that targets a specific constructor: `container.Register(() => new TelemetryClient(/*whatever constructor you want to target*/), requestOrTransientLifestyle);`. Also check out [DefaultScopedLifestyle](https://simpleinjector.readthedocs.org/en/latest/lifetimes.html#scoped) – qujck Nov 19 '15 at 23:32
  • 2
    For unit testing you should really define your own abstraction over TelemetryClient that you can mock as required. Unit tests should not be talking to Azure. – qujck Nov 19 '15 at 23:44
  • Your custom hybrid scope worries me. Mixing the web request lifestyle with a transient lifestyle is usually not a good practice. It can explain why you need this mixed lifestyle for? – Steven Nov 20 '15 at 06:22
  • @Steven - the hybrid scope was created as a way to deal with signalR needing to access the same service that the controller was using, but the service had been going out of scope. That was done by another developer, and since my app is not using signalR, I have since removed it. – Randy Gamage Nov 22 '15 at 18:50

5 Answers5

28

Microsoft.ApplicationInsights.TelemetryClient, which has no Interface and is a sealed class, with 2 constructors.

This TelemetryClient is a framework type and framework types should not be auto-wired by your container.

I found a post showing how to override the container's constructor resolution behavior, but that seems pretty complicated.

Yep, this complexity is deliberate, because we want to discourage people from creating components with multiple constructors, because this is an anti-pattern.

Instead of using auto-wiring, you can, as @qujck already pointed out, simply make the following registration:

container.Register<TelemetryClient>(() => 
    new TelemetryClient(/*whatever values you need*/),
    requestOrTransientLifestyle);

Or is Application Insights smart enough to know it is running in a unit test, and not send the data?

Very unlikely. If you want to test the class that depends on this TelemetryClient, you better use a fake implementation instead, to prevent your unit test to either become fragile, slow, or to pollute your Insight data. But even if testing isn't a concern, according to the Dependency Inversion Principle you should depend on (1) abstractions that are (2) defined by your own application. You fail both points when using the TelemetryClient.

What you should do instead is define one (or perhaps even multiple) abstractions over the TelemetryClient that are especially tailored for your application. So don't try to mimic the TelemetryClient's API with its possible 100 methods, but only define methods on the interface that your controller actually uses, and make them as simple as possible so you can make both the controller's code simpler -and- your unit tests simpler.

After you defined a good abstraction, you can create an adapter implementation that uses the TelemetryClient internally. I image you register this adapter as follows:

container.RegisterSingleton<ITelemetryLogger>(
    new TelemetryClientAdapter(new TelemetryClient(...)));

Here I assume that the TelemetryClient is thread-safe and can work as a singleton. Otherwise, you can do something like this:

container.RegisterSingleton<ITelemetryLogger>(
    new TelemetryClientAdapter(() => new TelemetryClient(...)));

Here the adapter is still a singleton, but is provided with a delegate that allows creation of the TelemetryClient. Another option is to let the adapter create (and perhaps dispose) the TelemetryClient internally. That would perhaps make the registration even simpler:

container.RegisterSingleton<ITelemetryLogger>(new TelemetryClientAdapter());
Steven
  • 151,500
  • 20
  • 287
  • 393
  • 2
    Thanks for this detailed answer. As Steve and @qujck suggested, my main question was answered with a simple one-liner to register the telemetryclient with the constructor of my choice (although Steve's answer has a typo - container.Register is duplicated). After posting this question I arrived at pretty much the same solution to the unit testing issue - wrapping TelemetryClient in a simple class of my own design, exposing the one method I need, created an interface for it, etc. Then in the unit testing, that simple wrapper class is Mocked with Moq. – Randy Gamage Nov 22 '15 at 18:47
26

Application Insights has an example of unit testing the TelemetryClient by mocking TelemetryChannel.

TelemetryChannel implements ITelemetryChannel so is pretty easy to mock and inject. In this example you can log messages, and then collect them later from Items for assertions.

public class MockTelemetryChannel : ITelemetryChannel
{
    public IList<ITelemetry> Items
    {
        get;
        private set;
    }

    ...

    public void Send(ITelemetry item)
    {
        Items.Add(item);
    }
}

...

MockTelemetryChannel = new MockTelemetryChannel();

TelemetryConfiguration configuration = new TelemetryConfiguration
{
    TelemetryChannel = MockTelemetryChannel,
    InstrumentationKey = Guid.NewGuid().ToString()
};
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

TelemetryClient telemetryClient = new TelemetryClient(configuration);

container.Register<TelemetryClient>(telemetryClient);
James Wood
  • 16,370
  • 2
  • 39
  • 80
  • And what would you use for the run time code? I tried to use `ServerTelemetryChannel`, but received a trace telemetry that "Server telemetry channel was not initialized. So persistent storage is turned off. You need to call ServerTelemetryChannel.Initialize()." But the intellisense on `Initialize()` says it's for internal use. – Tsahi Asher Aug 28 '18 at 06:56
  • Correction: But `Initialize()` Accepts a `TelemetryConfiguration`, which is what we're trying to create here. – Tsahi Asher Aug 28 '18 at 08:02
  • @TsahiAsher, I would suggest posting a new question for that one. – James Wood Aug 28 '18 at 08:17
  • https://stackoverflow.com/a/33820340/141022 has a persuasive argument for creating a sound abstraction around a 3rd party dependency, it's worth noting the AppInsights team (albeit an old post) do also recommend using the channel route https://github.com/Microsoft/ApplicationInsights-dotnet/issues/342 (at least at the moment, they might be considering new approaches). – Alex KeySmith Jan 11 '19 at 10:47
17

I had a lot of success with using Josh Rostad's article for writing my mock TelemetryChannel and injecting it into my tests. Here's the mock object:

public class MockTelemetryChannel : ITelemetryChannel
{
    public ConcurrentBag<ITelemetry> SentTelemtries = new ConcurrentBag<ITelemetry>();
    public bool IsFlushed { get; private set; }
    public bool? DeveloperMode { get; set; }
    public string EndpointAddress { get; set; }

    public void Send(ITelemetry item)
    {
        this.SentTelemtries.Add(item);
    }

    public void Flush()
    {
        this.IsFlushed = true;
    }

    public void Dispose()
    {

    }
}    

And then in my tests, a local method to spin-up the mock:

private TelemetryClient InitializeMockTelemetryChannel()
{
    // Application Insights TelemetryClient doesn't have an interface (and is sealed)
    // Spin -up our own homebrew mock object
    MockTelemetryChannel mockTelemetryChannel = new MockTelemetryChannel();
    TelemetryConfiguration mockTelemetryConfig = new TelemetryConfiguration
    {
        TelemetryChannel = mockTelemetryChannel,
        InstrumentationKey = Guid.NewGuid().ToString(),
    };

    TelemetryClient mockTelemetryClient = new TelemetryClient(mockTelemetryConfig);
    return mockTelemetryClient;
}

Finally, run the tests!

[TestMethod]
public void TestWidgetDoSomething()
{            
    //arrange
    TelemetryClient mockTelemetryClient = this.InitializeMockTelemetryChannel();
    MyWidget widget = new MyWidget(mockTelemetryClient);

    //act
    var result = widget.DoSomething();

    //assert
    Assert.IsTrue(result != null);
    Assert.IsTrue(result.IsSuccess);
}
spottedmahn
  • 11,379
  • 7
  • 75
  • 144
Jason Marsell
  • 1,512
  • 1
  • 16
  • 10
  • 1
    thank you for this very clean and easy to implement solution! thanks in addition for your non-smarty and friendly wording of your answer. Every other answer has some "container" code which I don't understand: My unit tests never have any "containers" to register types. this answer should be the accepted unswer. – schlingel Sep 02 '20 at 09:06
  • Exactly. I agree that this answer is more straightforward. I also had success using a very similar approach. – Mário Meyrelles Mar 10 '21 at 18:20
4

Another option without going the abstraction route is to disable telemetry before doing running your tests:

TelemetryConfiguration.Active.DisableTelemetry = true;

humbleice
  • 666
  • 6
  • 12
3

If you don't want to go down the abstraction / wrapper path. In your tests you could simply direct the AppInsights endpoint to a mock lightweight http server (which is trivial in ASP.NET Core).

appInsightsSettings.json

    "ApplicationInsights": {
    "Endpoint": "http://localhost:8888/v2/track"
}

How to set up "TestServer" in ASP.NET Core http://josephwoodward.co.uk/2016/07/integration-testing-asp-net-core-middleware

DalSoft
  • 9,073
  • 3
  • 33
  • 47