ElasticSearch 2.0 Nest Unit Testing with MOQ [英] ElasticSearch 2.0 Nest Unit Testing with MOQ
问题描述
我在使用 ElasticSearch 和 Nest 为搜索创建单元测试时遇到问题.
I'm having problems creating Unit test for the search using ElasticSearch with Nest.
单元测试
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;
结果始终为空,即使我执行以下操作
The result is always null, even if I do the following
var temp = ConnectionClient.Search<Person>(s => s.Index("person_index").Query(q => q.Term(t => t.Id, searchValue)));
任何帮助将不胜感激.
推荐答案
Func
的签名传递给 It.IsAny
不正确,因此设置期望永远不会匹配.签名应该是
The signature of the Func<T1, T2>
passed to It.IsAny<T>()
is not correct so the setup expectation will never be matched. The signature should be
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
If you don't need to stub the client then you can simply use a real client and set the IConnection
to an instance of 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}
" +
$"{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}
" +
$"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}
" +
$"{new string('-', 30)}
");
}
else
{
Console.WriteLine($"Status: {response.HttpStatusCode}
" +
$"{new string('-', 30)}
");
}
});
var client = new ElasticClient(connectionSettings);
如果需要,您还可以通过这种方式捕获请求.您可以更进一步,创建您自己的 IConnection
实现以返回存根响应.
This way you could also capture the requests if you needed to. You could take this a step further and create your own IConnection
implementation that returns stub responses.
这篇关于ElasticSearch 2.0 Nest Unit Testing with MOQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!