ElasticSearch 2.0 带最小起订量的嵌套单元测试
ElasticSearch 2.0 Nest Unit Testing with MOQ
我在使用带 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)));
如有任何帮助,我们将不胜感激。
传递给 It.IsAny<T>()
的 Func<T1, T2>
的签名不正确,因此永远不会匹配设置预期。签名应该是
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;}
}
如果您不需要 stub 客户端,那么您可以简单地使用真实客户端并将 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
实现,returns 存根响应。
我在使用带 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)));
如有任何帮助,我们将不胜感激。
传递给 It.IsAny<T>()
的 Func<T1, T2>
的签名不正确,因此永远不会匹配设置预期。签名应该是
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;}
}
如果您不需要 stub 客户端,那么您可以简单地使用真实客户端并将 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
实现,returns 存根响应。