繁体   English   中英

单元测试Elasticsearch.net IElasticLowLevelClient

[英]UnitTesting Elasticsearch.net IElasticLowLevelClient

我有一个非常简单的类,该类使用Elasticsearch.net通过简单的查询访问端点。 该类和方法工作正常,我得到了预期的结果。 但是我无法在此类的UnitTesting中成功。 这是我要进行单元测试的类:

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 :(
        }
    }
}
}

界面:

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

我用来尝试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);
    }

我遇到的错误是:

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

我需要设置成功属性和body属性,但我没有看到如何使用所涉及对象的复杂结构来设置body。

有人有解决方案或可以看到我做错了吗? 谢谢。

附言:请忽略命名。 我故意将它们更改为这些无意义的名称。

从查看源( 在GitHub上Elasticsearch网 ),你应该能够创建的实例StringResponse直接传入Body的构造函数。 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);
}

我已经从上面复制了lowLevelClient设置的代码(删除了多余的逗号),但是如果我们想检查搜索是否被正确调用,我们应该将它们与实际参数匹配,而不是使用()。

暂无
暂无

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

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