栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

最小起订量的ElasticSearch 2.0嵌套单元测试

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

最小起订量的ElasticSearch 2.0嵌套单元测试

Func<T1, T2>
传递给的签名
It.IsAny<T>()
不正确,因此设置期望将永远不会匹配。签名应为

It.IsAny<Func<SearchDescriptor<Person>, ISearchRequest>>()

一个完整的工作示例

void Main(){    var people = new List<Person>    {        new Person { Id = 1 },        new Person { Id = 2 },    };    var mockSearchResponse = new Mock<ISearchResponse<Person>>();    mockSearchResponse.Setup(x => x.documents).Returns(people);    var mockElasticClient = new Mock<IElasticClient>();    mockElasticClient.Setup(x => x        .Search(It.IsAny<Func<SearchDescriptor<Person>, ISearchRequest>>()))        .Returns(mockSearchResponse.Object);    var result = mockElasticClient.Object.Search<Person>(s => s);    Assert.AreEqual(2, result.documents.Count()).Dump();}public class Person{    public int Id { get; set;}}

如果您不需要 存根 客户端,则只需使用真实客户端并将设置

IConnection
为的实例即可
InMemoryConnection

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));// pass an instance of InMemoryConnection so that requests are not // **actually** sentvar connectionSettings = new ConnectionSettings(pool, new InMemoryConnection())        .PrettyJson()        .DisableDirectStreaming()        .onRequestCompleted(response => {     // log out the request     if (response.RequestBodyInBytes != null)     {         Console.WriteLine(  $"{response.HttpMethod} {response.Uri} n" +  $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");     }     else     {         Console.WriteLine($"{response.HttpMethod} {response.Uri}");     }     // log out the response     if (response.ResponseBodyInBytes != null)     {         Console.WriteLine($"Status: {response.HttpStatusCode}n" +       $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}n" +       $"{new string('-', 30)}n");     }     else     {         Console.WriteLine($"Status: {response.HttpStatusCode}n" +       $"{new string('-', 30)}n");     } });var client = new ElasticClient(connectionSettings);

这样,您还可以根据需要捕获请求。您可以更进一步,并创建自己的

IConnection
实现,该实现返回存根响应。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393237.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号