简体   繁体   English

单元测试 Azure 函数:无法创建 TraceWriter 的实例,如何模拟?

[英]Unit testing Azure Function: Cannot create an instance of TraceWriter, how to mock?

I'm trying to unit test a basic Azure Function.我正在尝试对基本的 Azure 函数进行单元测试。 The function's Run method requires a TraceWriter argument;该函数的 Run 方法需要一个 TraceWriter 参数; TraceWriter is an abstract class and I'm not finding much in terms of documentation for mocking this dependency. TraceWriter 是一个抽象类,我在模拟这种依赖项的文档方面没有找到太多内容。

Here's the signature of the method I'm attempting to test:这是我尝试测试的方法的签名:

public static void Run(string myQueueItem, TraceWriter log)

Any insights regarding mocking TraceWriter and/or Azure Function unit testing strategies would be much appreciated.任何有关模拟 TraceWriter 和/或 Azure 函数单元测试策略的见解将不胜感激。

Azure Functions now can support consuming an ILogger as per this GitHub thread: https://github.com/Azure/Azure-Functions/issues/293 Azure Functions 现在可以支持按照此 GitHub 线程使用 ILogger: https : //github.com/Azure/Azure-Functions/issues/293


My suggestion would be that you use the new tooling supported in VS2017 Preview with precompiled functions to allow you to improve your function's testability.我的建议是您使用 VS2017 Preview 中支持的新工具和预编译函数,以提高函数的可测试性。 You can get started with the new tools for Azure Functions here:你可以在此处开始使用 Azure Functions 的新工具:

https://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a-function-app/ https://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a-function-app/

Donna Malayeri has published an excellent post that explains how to use precompiled Functions with C#: https://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a-function-app/ Donna Malayeri 发表了一篇出色的文章,解释了如何在 C# 中使用预编译函数: https ://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a -功能应用程序/

This will allow you to create a Function that consumes an Interface instead of the concrete object.这将允许您创建一个使用接口而不是具体对象的函数。 The answer is a bit long winded but there's a similar thread here with a nice answer:答案有点啰嗦,但这里有一个类似的线程,有一个很好的答案:

Azure Function logging using TraceWriter in external library 在外部库中使用 TraceWriter 进行 Azure 函数日志记录

ILogger 日志记录器

use Moq;使用最小起订量;

... ...

... ...

var log = new Mock(TraceLevel.Info).Object; var log = new Mock(TraceLevel.Info).Object;

Simple!简单的!

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

相关问题 在天蓝色功能V2中用ILogger替换TraceWriter - Replace TraceWriter With ILogger in azure function V2 模拟数据库 function 进行单元测试 - Mock a database function for unit testing 在外部库中使用 TraceWriter 进行 Azure 函数日志记录 - Azure Function logging using TraceWriter in external library 如何模拟 gremlin 服务器或创建用于单元测试的内存图? - How to mock a gremlin server or create in-memory graph for unit testing? 如何模拟WebOperationContext进行单元测试? - How to mock WebOperationContext for unit testing? 如何模拟在单元测试功能中使用的SmtpClient对象 - how to mock the SmtpClient object which is used inside a function for Unit Testing 不接受Azure功能(ILogger或TraceWriter)的上次日志记录参数 - Last logging parameter of Azure Function (ILogger or TraceWriter) not accepted 如何模拟 IOptionsSnapshot 实例进行测试 - How to mock IOptionsSnapshot instance for testing 在没有应用程序见解的情况下在Azure函数中查看TraceWriter.Info()日志 - View TraceWriter.Info() logs in Azure Function without Application Insights Mock JsonReader单元如何测试自定义JsonConverter - How Mock JsonReader unit testing a custom JsonConverter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM