elasticsearch,moq,C#,Unit Testing,elasticsearch,Moq" /> elasticsearch,moq,C#,Unit Testing,elasticsearch,Moq" />

C# 单元测试Elasticsearch.net IELASTICLOWLEVEL客户端

C# 单元测试Elasticsearch.net IELASTICLOWLEVEL客户端,c#,unit-testing,elasticsearch,moq,C#,Unit Testing,elasticsearch,Moq,我有一个非常简单的类,它使用Elasticsearch.net通过简单的查询访问端点。该类和方法运行良好,我得到了预期的结果。但是我在单元测试这门课上没有成功。 下面是我要测试的类: namespace X { public class YLookup : IYLookup { private readonly IElasticLowLevelClient _lowLevelClient; public YLookup() { } public YLoo

我有一个非常简单的类,它使用Elasticsearch.net通过简单的查询访问端点。该类和方法运行良好,我得到了预期的结果。但是我在单元测试这门课上没有成功。 下面是我要测试的类:

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 :(
        }
    }
}
}
    [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);
    }
我用于尝试进行单元测试的代码:

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 :(
        }
    }
}
}
    [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);
    }
我需要设置success属性和body属性,但我不知道如何设置包含复杂对象结构的body

是否有人有解决方案或能看出我做错了什么? 谢谢


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

通过查看源代码(),您应该能够直接创建
StringResponse
的实例,并在ctor中传递
主体。
ElasticsearchResponseBase
上的
ApiCall
属性有一个公共get/set对,因此您应该能够按照

[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);
}
[TestMethod]
公共void Test1()
{
字符串h=“{\r\n\t\”take\”:2、\r\n\t\”timed\u out\”:false、\r\n\t\“\u shards\”:{\r\n\t\t\”total\”:6、\r\n\t\t\”successful\”:6、\r\n\t\t\”failed\”:0\r\n\t\t\t\t\t\”:{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n;
Mock apiCallDetails=newmock(MockBehavior.Strict);
Setup(x=>x.Success).Returns(true);
var resp=新的StringResponse(h);
resp.ApiCall=apiCallDetails.Object;
Mock elasticLowLevelClient=新的Mock(MockBehavior.Strict);
elasticLowLevelClient.Setup(x=>x.Search(It.IsAny(),It.IsAny(),It.IsAny(),It.IsAny())。返回(resp);
}

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

乍一看,我猜失败的是
elasticsearchResponse.Setup
s.Setup
。你能证实这一点吗?如果是这样,那么这些很可能是您试图设置的成员不是虚拟的-无法模拟。你可以只实例化它们的实例吗?它们都失败了。我尝试了模拟对象和实例化对象的不同组合,但没有效果。它真的不喜欢为它设置body属性。这有点尴尬。不知何故,我没有看到将主体作为参数的构造函数。这就解决了问题。谢谢如果没有其他问题,我最终会接受这个答案。