简体   繁体   English

如何为C#Azure功能编写单元测试?

[英]How do I write Unit Test for C# Azure Function?

I just started writing some C# function code these days and I have to send track event using Application Insight(AI). 我这些天刚开始写一些C#功能代码,我必须使用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. 我为Run方法的四个参数创建了一些mock类,并且已经覆盖了它的方法。 This was easy. 这很容易。 but I don't know how to mock TelemetryClient#TrackEvent because I NEW that instance in the Run method. 但我不知道如何模拟TelemetryClient#TrackEvent因为我在Run方法中新建了该实例。

I saw the page below using DI for this but I couldn't understand that how to write unit test properly. 我看到下面的页面使用DI,但我无法理解如何正确编写单元测试。

Using Application Insights with Unit Tests? 将Application Insights与单元测试结合使用?

So can you show me the example unit test code for this? 那么你能告诉我这个示例单元测试代码吗?

First of all, Azure Functions supports Application Insights out-of-the-box. 首先,Azure Functions支持开箱即用的Application Insights。

Azure Functions now has direct integration with Application Insights Azure Functions现在可以与Application Insights直接集成

Therefore, I wouldn't recommend you to directly implement TelemetryClient in your code. 因此,我不建议您在代码中直接实现TelemetryClient Instead, replace your TraceWriter parameter to ILogger to get benefits from Application Insights. 而是将TraceWriter参数替换为ILogger以从Application Insights中获益。

However, if you really want to use the TelemetryClient within your code, I would recommend creating a wrapper interface like ITelemetryClientWrapper , implementing it and injecting it through the dependency injection approach. 但是,如果您真的想在代码中使用TelemetryClient ,我建议您创建一个类似ITelemetryClientWrapper的包装器接口,实现它并通过依赖注入方法注入它。

I've written a blog post about dependency injection around Azure Functions: 我写了一篇关于Azure Functions的依赖注入的博客文章:

Azure Functions with IoC Container 使用IoC容器的Azure功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为用C#编写的Azure Function应用编写单元测试? - How can i write unit test for Azure Function App written in c#? 当 function 必须写入标准输出时,我如何对 C# function 进行单元测试 - How do I unit test a C# function when that function must write to stdout 如何使用 Ms unit project c# 代码为逻辑应用程序步骤编写单元测试? - How do I write unit test for logic apps steps using Ms unit project c# code? Azure Function C# 中的单元测试 - Unit Test in Azure Function C# 如何对返回FUNC的C#函数进行单元测试 <something> ? - How do I unit test a C# function which returns a Func<something>? 我如何在c#中对单元测试的URL有效? - How do I Unit test is URL valid in c#? 如何正确使用 C# 单元测试 FakeHttpContext? - How do I use C# Unit test FakeHttpContext correctly? I created this Http Azure function with SendGrid and not sure how to write a unit test to call it with different email test case - I created this Http Azure function with SendGrid and not sure how to write a unit test to call it with different email test case 如何编写Azure功能v1的单元测试 - How to write unit test for azure function v1 如何为具有带参数运行方法的 azure 函数编写单元测试? - How can I write unit test for azure function which has run method with parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM