我在使用带有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());
Run Code Online (Sandbox Code Playgroud)
工作代码
results = ConnectionClient.Search<Person>(s => s.Index("person_index").Query(q => q.Term(t => t.Id, searchValue))).Documents;
Run Code Online (Sandbox Code Playgroud)
即使我执行以下操作,结果也始终为null
var temp = ConnectionClient.Search<Person>(s => s.Index("person_index").Query(q => q.Term(t => t.Id, searchValue)));
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
我正在尝试围绕我们对 ElasticSearch 的使用添加测试(在 C# 中使用 Nest 1.4.2)并想使用 InMemoryConnection 但我错过了一些东西(我假设)并且没有成功。
我创建了这个简单的 Nunit 测试用例作为我问题的一个简化示例
using System;
using Elasticsearch.Net.Connection;
using FluentAssertions;
using Nest;
using NUnit.Framework;
namespace NestTest
{
public class InMemoryConnections
{
public class TestThing
{
public string Stuff { get; }
public TestThing(string stuff)
{
Stuff = stuff;
}
}
[Test]
public void CanBeQueried()
{
var connectionSettings = new ConnectionSettings(new Uri("http://foo.test"), "default_index");
var c = new ElasticClient(connectionSettings, new InMemoryConnection(connectionSettings));
c.Index(new TestThing("peter rabbit"));
var result = c.Search<TestThing>(sd => sd);
result.ConnectionStatus.Success.Should().BeTrue();
}
}
} …
Run Code Online (Sandbox Code Playgroud)