NEST中的Mock Elasticsearch客户端存在异步搜索方法问题



我使用NEST框架在c#中进行弹性搜索查询。

我正在用Moq编写一个简单的单元测试。

mockElasticClient.Setup(ec => ec.SearchAsync(
It.IsAny<Func<SearchDescriptor<Relativity>,
SearchDescriptor<Relativity>>>()))
.ReturnsAsync(mockSearchResponse.Object);

上述代码抛出'An expression tree may not contain a call or invocation that uses optional arguments'

如果我使用Search而不是SearchAsync,那么上面的代码就可以工作了。

我在这里做错了什么?

模拟函数中的所有参数都应该被考虑在内。在这种情况下,根据功能定义

//
// Parameters:
//   selector:
//     A descriptor that describes the parameters for the search operation
//
// Type parameters:
//   T:
//     The type used to infer the index and typename as well describe the query strongly
//     typed
Task<ISearchResponse<T>> SearchAsync<T>(Func<SearchDescriptor<T>, ISearchRequest> selector = null, CancellationToken cancellationToken = default) where T : class;

存在两个参数Func<SearchDescriptor,ISearchRequest>选择器和CancellationToken。

模拟要求CancellationToken在下方进行模拟

mockElasticClient.Setup(x => x
.SearchAsync(It.IsAny<Func<SearchDescriptor<Relativity>, ISearchRequest>>(),It.IsAny<CancellationToken>()))
.ReturnsAsync(mockSearchResponse.Object);

最新更新