I just started writing some C# function code these days and I have to send track event using Application Insight(AI). Here is the sample code that I wrote.
namespace BlobTrigger {
public static class Main {
private static string sKey = TelemetryConfiguration.Active.InstrumentationKey = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);
private static TelemetryClient sTelemetry;
[FunctionName("BlobTrigger")]
public static void Run(
[BlobTrigger("upload/{name}.wav")] Stream myBlob,
string name,
Microsoft.Azure.WebJobs.ExecutionContext context,
TraceWriter log) {
sTelemetry = new TelemetryClient() { InstrumentationKey = sKey };
sTelemetry.Context.Operation.Id = context.InvocationId.ToString();
sTelemetry.Context.Operation.Name = name;
sTelemetry.TrackEvent("File is uploaded");
.....
}
}
This function works fine. But my problem is writing some unit test for this. I create some mock class for four arguments of the Run method and overrode its method already. This was easy. but I don't know how to mock TelemetryClient#TrackEvent
because I NEW that instance in the Run method.
I saw the page below using DI for this but I couldn't understand that how to write unit test properly.
Using Application Insights with Unit Tests?
So can you show me the example unit test code for this?