c# - 如何模拟 Elasticsearch .Net 客户端?
问题描述
我正在使用 Elasticsearch .Net Client 6.x,我有一些代码,例如:
if(this.elasticClient.IndexExists("indexName").Exists){
// do something
}
this.elasticClient.CreateIndex(
newIndex,
x => x.Settings(s => s.NumberOfShards(1)).Mappings(ms => ms.Map<T>(m => m.AutoMap())));
var searchResponse = client.Search<dynamic>(
s => s.AllTypes()
.Index(
new[] { "indexName1", "indexName"}).IgnoreUnavailable().Size(size).From(from).Query(
q => q.Bool(b => b.Must(m => m.SimpleQueryString(c => c.Query(query).Lenient().AnalyzeWildcard())))));
现在在我的单元测试中,我想测试descriptor
传递给每个方法的确切内容,我该怎么做?
var existsResponse = new Mock<ExistsResponse>();
existsResponse.Setup(x => x.Exists).Returns(false); // Oops: can't do this since Exists is not virtual, abstract
this.elasticClient.Setup(x => x.IndexExists("indexName", null)).Returns(existsResponse.Object);
this.elasticClient.Verify(
x => x.CreateIndex("IndexName", It.Is<Func<CreateIndexDescriptor, ICreateIndexRequest>>(x => /*what goes here??*/)),
Times.Once);
this.elasticClient.Verify(
x => x.Search<dynamic>(It.Is<Func<SearchDescriptor<dynamic>>>(x => /*what goes here??*/)),
Times.Once);
我是否应该简单地将 elasticClient 包装在一个类中并实现我使用的每个方法,并将其作为依赖注入传递?
解决方案
推荐阅读
- python - 有没有一种简单的方法来重复调用 LSTM 模型而不是在 Tensorflow 中传递一个序列?
- solr - 挖掘 SOLR 查询日志文件以计算每月的最高(最频繁)搜索
- javascript - 滚动条从一个网站到另一个网站保持在同一位置
- hdf5 - 如何更改 automake 安装 libtool 库的位置?
- ionic-framework - 使用把手改变字体大小
- python - 如何在 Python 中从 Informix 和 Oracle 数据库中检索图像?
- arrays - 使用 SwiftyJSON 反序列化时如何添加对象数组
- c# - 基类中的 Polly 使用参数执行抽象方法
- ssl - 在建立与网站的连接时,如何确保域级 SSL 证书存在于信任库中?
- css - 渲染一个组件时,隐藏路由器中兄弟 div 的 HTML 中的空格/HTML 溢出?