c# - 使用 ISearchIndexClient 进行测试

标签 c# unit-testing azure-cognitive-search

我想测试我的功能。它们全部依赖于ISearchIndexClient 。我试图创建一个 Mock<ISearchIndexClient>但我无法使用它,因为 ISearchIndexClient 不接受模拟.

在我的函数中,我正在调用

DocumentSearchResult<SearchData> result = 
                           await client.Documents.SearchAsync<SearchData>(searchText, sp);

然后,我尝试将自定义客户端创建为

public class CustomSearchIndexClient : ISearchIndexClient { }

public class CustomDocumentsOperations : IDocumentsOperations { }

但我无法覆盖 SearchAsync<T> .

如何测试我的功能?谢谢

最佳答案

SearchAsync是一种很难模拟的扩展方法。如果您可以检查源代码并找出扩展方法正在调用哪些实例成员,您可以模拟它,但这对于一个简单的测试来说是很多工作

我建议抽象出这种紧密耦合的依赖关系,以便您的函数更易于测试。

将 Azure 特定代码包装在您控制的抽象后面,仅公开所需的功能,这应该使您能够更灵活地模拟外部依赖项,以便在测试时正确执行您的功能,

关于c# - 使用 ISearchIndexClient 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51821684/

相关文章:

python - 通过模拟 IP 套接字、串行端口和共享库来测试应用程序的有效测试策略是什么?

azure-cognitive-search - Azure 搜索 : What is the best way to update a batch of documents

python - 如果未引发异常,则通过 Python 单元测试

odata - Azure 搜索 : Group By or Distinct in OData context/Query?

c# - azure 搜索服务和开放时间

c# - RowHeaderTemplateSelector 对象参数为空

C# 串行端口在单次传输中未接收到所有字节

c# - 如何在 Asp.net Core 中通过 ILogger<T> 将日志写入 EventLog?

c# - 从C#中的Hive流脚本访问文件(HDInsight)

unit-testing - 如何使用 hsqdb 模拟数据库中断?