相关疑难解决方法(0)

使用MOQ进行ElasticSearch 2.0 Nest单元测试

我在使用带有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)

任何帮助,将不胜感激.

.net unit-testing elasticsearch nest

9
推荐指数
1
解决办法
3410
查看次数

Elasticsearch DeleteByQuery无法正常工作,获得400个错误请求

我有以下的Nest查询来删除所有匹配的文档,非常直接,但我收到了400个错误的请求.

 var client = new ElasticClient();
        var request = new DeleteByQueryRequest<Type>("my-index")
        {
            Query = new QueryContainer(
                    new TermQuery
                    {
                        Field = "versionId",
                        Value = "ea8e517b-c2e3-4dfe-8e49-edc8bda67bad"
                    }
                )
        };
        var response = client.DeleteByQuery(request);
        Assert.IsTrue(response.IsValid);
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

---------------更新---------------

请求机构

{"query":{"term":{"versionId":{"value":"ea8e517b-c2e3-4dfe-8e49-edc8bda67bad"}}}}
Run Code Online (Sandbox Code Playgroud)

响应机构

{"took":0,"timed_out":false,"_indices":{"_all":{"found":0,"deleted":0,"missing":0,"failed":0}},"failures":[]}
Run Code Online (Sandbox Code Playgroud)

在Sense插件中查询:

GET /my-index/type/_search
{
  "query": {

          "match": {
             "versionId": "ea8e517b-c2e3-4dfe-8e49-edc8bda67bad"
          } 

  }
}
Run Code Online (Sandbox Code Playgroud)

查询响应:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 116,
      "max_score": 2.1220484,
      "hits": []
...
}}
Run Code Online (Sandbox Code Playgroud)

--------------- NEST QUERY …

.net c# elasticsearch nest

8
推荐指数
1
解决办法
686
查看次数

使用Nest在C#中进行Elasticsearch测试(单元/集成)最佳实践

我一直在搜索我应该如何测试我的数据访问层而不是很成功.让我列出我的担忧:

  1. 单元测试

这个人(这里:使用InMemoryConnection测试ElasticSearch)说:

虽然声明序列化形式的请求符合您在SUT中的期望可能就足够了.

断言请求的序列化形式是否真的值得?这些测试有什么价值吗?它似乎不太可能改变不应该更改序列化请求的函数.

如果值得,那么断言这些要求的正确方法是什么?

  1. 再次进行单元测试

另一个人(这里:使用MOQ的ElasticSearch 2.0 Nest Unit Testing)显示了一个好看的例子:

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;}
}
Run Code Online (Sandbox Code Playgroud)

可能我错过了一些东西,但我看不出这段代码的重点.首先,他嘲笑一个 …

testing elasticsearch nest

4
推荐指数
1
解决办法
2481
查看次数

标签 统计

elasticsearch ×3

nest ×3

.net ×2

c# ×1

testing ×1

unit-testing ×1