模拟 Microsoft Application Insights API

Pra*_*een 6 unit-testing mocking azure azure-application-insights

我有一个应用程序,它使用自定义跟踪/指标写入 App INsights ,还使用​​REST API将数据读取到仪表板上。 我的问题是对于我的单元测试,我可以模拟自定义跟踪(TelemetryClient)和 REST API 吗?

我看到 REST API 有一个演示版本,但提供随机信息。如果我可以设置一个 DEMO 仪器密钥来写入和读取它以进行单元测试,那将会很有帮助。让我知道。

Ene*_*nes 5

My proffered approach would be (as for any external component) to create some sort of wrapper around Telemetry Client and then it would be easy to mock it or replace it later if needed.

The other approach I would try is to use TelemetryClient constructor overload with TelemetryConfiguration and mock TelemetryChannel.

var client = new TelemetryClient(
    new TelemetryConfiguration
    {                   
         TelemetryChannel = new MOCK...
    });
Run Code Online (Sandbox Code Playgroud)