gpt4 book ai didi

c# - 表达式引用了不属于 ISearchIndexClient.Documents.Search 方法的模拟对象的方法

转载 作者:行者123 更新时间:2023-12-02 08:04:19 25 4
gpt4 key购买 nike

我正在使用 Azure 搜索,这是我的单元测试代码:

var expectedResponse = new DocumentSearchResult { };
var _searchIndexRepository = new Mock<ISearchIndexClient>();
_searchIndexRepository
.Setup(r => r.Documents.Search(It.IsAny<string>(), It.IsAny<SearchParameters>(), It.IsAny<SearchRequestOptions>()))
.Returns(expectedResponse);

设置时出现的错误是:

Expression references a method that does not belong to the mocked object

有办法让它工作吗?

最佳答案

感谢您的建议。以下解决方法解决了我的问题:

我为 SearchIndexClient 创建了一个包装器,如下所示:

public interface ICustomSearchIndexClient
{
DocumentSearchResult<T> Search<T>(string searchTerm, SearchParameters parameters) where T : class;
}

public class CustomSearchIndexClient : ICustomSearchIndexClient
{
private readonly SearchIndexClient _searchIndexClient;

public CustomSearchIndexClient(string searchServiceName, string indexName, string apiKey)
{
_searchIndexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(apiKey));
}

public DocumentSearchResult<T> Search<T>(string searchTerm, SearchParameters parameters) where T: class
{
return _searchIndexClient.Documents.Search<T>(searchTerm, parameters);
}
}

更改业务逻辑如下:

构造函数:

public CustomSearchService(string serviceName, string apiKey, string indexName, ICustomSearchIndexClient customSearchIndexClient)
{
_serviceName = serviceName;
_apiKey = apiKey;
_indexName = indexName;
_customSearchIndexClient = customSearchIndexClient;
}

搜索方法:

public DocumentSearchResult<CustomSearchResult> Search(string search)
{
return _customSearchIndexClient.Search<CustomSearchResult>(string.IsNullOrEmpty(search) ? "*" : search, null)
}

像这样更改了我的单元测试:

[TestCategory("UnitTest")]
[TestMethod]
public void SearchTest()
{
//Arrange
var expectedResponse = new DocumentSearchResult<Models.CustomSearchResult> { Count = 1, Results = <instantiate your custom model here>, Facets = < instantiate your custom model here > };

var searchIndexClient = new Mock<ICustomSearchIndexClient>();
searchIndexClient.Setup(r => r.Search<Models.CustomSearchResult>(It.IsAny<string>(), null)).Returns(expectedResponse);
var business = new CustomSearchService("serviceName", "apiKey", "indexname", searchIndexClient.Object);

//Act
var result = business.Search("search term");

//Assert
Assert.IsNotNull(result, "Business logic method returned NULL");
}

使用 ninject 将包装器 ICustomSearchIndex 注入(inject)到 CustomSearchService 业务逻辑中:

Bind<ICustomSearchIndexClient>().To<CustomSearchIndexClient>();
Bind<ICustomSearchService>().To<CustomSearchService>()
.WithConstructorArgument("serviceName", ConfigurationManager.AppSettings["SearchServiceName"])
.WithConstructorArgument("apiKey", ConfigurationManager.AppSettings["SearchServiceApiKey"])
.WithConstructorArgument("indexName", ConfigurationManager.AppSettings["IndexName"]);

关于c# - 表达式引用了不属于 ISearchIndexClient.Documents.Search 方法的模拟对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48015912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com