一尘不染

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

elasticsearch

我在使用带有Nest的ElasticSearch创建用于搜索的单元测试时遇到问题。

单元测试

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>, SearchRequest<Person>>>())).Returns(mockSearchResponse.Object);
var service = new PersonService(mockElasticClient.Object);
var result = service.Search(string.Empty, string.Empty);
Assert.AreEqual(2,result.Count());

工作代码

results = ConnectionClient.Search<Person>(s => s.Index("person_index").Query(q => q.Term(t => t.Id, searchValue))).Documents;

即使执行以下操作,结果也始终为空

var temp = ConnectionClient.Search<Person>(s => s.Index("person_index").Query(q => q.Term(t => t.Id, searchValue)));

任何帮助,将不胜感激。


阅读 223

收藏
2020-06-22

共1个答案

一尘不染

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** sent
var 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实现,该实现返回存根响应。

2020-06-22