简体   繁体   English

单元测试Elasticsearch.net IElasticLowLevelClient

[英]UnitTesting Elasticsearch.net IElasticLowLevelClient

I have a very simple class that uses Elasticsearch.net to access an endpoint with a simple query. 我有一个非常简单的类,该类使用Elasticsearch.net通过简单的查询访问端点。 The class and method works fine and I get the expected results. 该类和方法工作正常,我得到了预期的结果。 But I am not successful in UnitTesting this class. 但是我无法在此类的UnitTesting中成功。 Here is the class I am trying to UnitTest: 这是我要进行单元测试的类:

namespace X
{
public class YLookup : IYLookup
{
    private readonly IElasticLowLevelClient _lowLevelClient;
    public YLookup()
    {

    }
    public YLookup(IElasticLowLevelClient lowLevelClient)
    {
        _lowLevelClient = lowLevelClient;
    }

    public string Lookup(string Z)
    {
        if (string.IsNullOrEmpty(Z))
        {
            return string.Empty;
        }
        var searchResponse = _lowLevelClient.Search<StringResponse>(
            "person",
            "company",
            "My elastic search query");

        if (searchResponse.Success)
        {
            // success! Parse searchResponse.Body;
        }
        else
        {
            // Failure :(
        }
    }
}
}

The interface: 界面:

namespace X
{
internal interface IYLookup
{
    string Lookup(string Z);
}
}

The code I am using to try to UnitTest: 我用来尝试UnitTest的代码:

    [TestMethod]
    public void Test1()
    {
        string h = "{\r\n\t\"took\": 2,\r\n\t\"timed_out\": false,\r\n\t\"_shards\": {\r\n\t\t\"total\": 6,\r\n\t\t\"successful\": 6,\r\n\t\t\"failed\": 0\r\n\t},\r\n\t\"hits\": {\r\n\t\t\"total\": 0,\r\n\t\t\"max_score\": null,\r\n\t\t\"hits\": []\r\n\t}\r\n}";

        Mock<IApiCallDetails> apiCallDetails = new Mock<IApiCallDetails>(MockBehavior.Strict);
        apiCallDetails.Setup(x => x.Success).Returns(true);

        Mock<ElasticsearchResponse<string>> elasticsearchResponse = new Mock<ElasticsearchResponse<string>>();
        elasticsearchResponse.Setup(x => x.Body).Returns(h);

        Mock<StringResponse> s = new Mock<StringResponse>();
        s.Setup(x => x.ApiCall).Returns(apiCallDetails.Object);

        Mock<IElasticLowLevelClient> elasticLowLevelClient = new Mock<IElasticLowLevelClient>(MockBehavior.Strict);

        elasticLowLevelClient.Setup(x => x.Search<StringResponse>(It.IsAny<string>(), It.IsAny<string>(),, It.IsAny<PostData>(), It.IsAny<SearchRequestParameters>())).Returns(s.Object);
    }

The error I am having is: 我遇到的错误是:

invalid setup on a non-virtual (overridable in vb) member

I need to set the success property and the body property and I am not seeing how I can set the body with the complicated structure of the objects involved. 我需要设置成功属性和body属性,但我没有看到如何使用所涉及对象的复杂结构来设置body。

Does someone have a solution or can see what I am doing wrong? 有人有解决方案或可以看到我做错了吗? Thanks. 谢谢。

PS Please ignore naming. 附言:请忽略命名。 I have changed them to these meaningless names on purpose. 我故意将它们更改为这些无意义的名称。

From looking at the source ( Elasticsearch-net on GitHub ) you should be able to create an instance of StringResponse directly, passing in the Body in the ctor. 从查看源( 在GitHub上Elasticsearch网 ),你应该能够创建的实例StringResponse直接传入Body的构造函数。 The ApiCall property on the ElasticsearchResponseBase has a public get/set pair so you should be able to do something along the lines of ElasticsearchResponseBase上的ApiCall属性具有公共的获取/设置对,因此您应该能够按照以下步骤进行操作

[TestMethod]
public void Test1()
{
    string h = "{\r\n\t\"took\": 2,\r\n\t\"timed_out\": false,\r\n\t\"_shards\": {\r\n\t\t\"total\": 6,\r\n\t\t\"successful\": 6,\r\n\t\t\"failed\": 0\r\n\t},\r\n\t\"hits\": {\r\n\t\t\"total\": 0,\r\n\t\t\"max_score\": null,\r\n\t\t\"hits\": []\r\n\t}\r\n}";

    Mock<IApiCallDetails> apiCallDetails = new Mock<IApiCallDetails>(MockBehavior.Strict);
    apiCallDetails.Setup(x => x.Success).Returns(true);

    var resp = new StringResponse(h);
    resp.ApiCall = apiCallDetails.Object;

    Mock<IElasticLowLevelClient> elasticLowLevelClient = new Mock<IElasticLowLevelClient>(MockBehavior.Strict);

    elasticLowLevelClient.Setup(x => x.Search<StringResponse>(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<PostData>(), It.IsAny<SearchRequestParameters>())).Returns(resp);
}

I have copied the code from above for the lowLevelClient setup (removing the extra comma) but if we want to check that search is being invoked correctly we should match them off to the actual parameters rather than using (). 我已经从上面复制了lowLevelClient设置的代码(删除了多余的逗号),但是如果我们想检查搜索是否被正确调用,我们应该将它们与实际参数匹配,而不是使用()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM