我有一个非常简单的类,它使用 Elasticsearch.net 通过简单查询访问端点。该类和方法工作正常,我得到了预期的结果。但是我在这个类的单元测试中没有成功。 这是我要进行单元测试的类(class):
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);
}
}
我用来尝试单元测试的代码:
[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
我需要设置 success 属性和 body 属性,但我看不到如何设置包含所涉及对象的复杂结构的 body。
有人有解决方案或可以看出我做错了什么吗? 谢谢。
附言请忽略命名。我故意把它们改成了这些毫无意义的名字。
最佳答案
通过查看源代码 (Elasticsearch-net on GitHub),您应该能够直接创建 StringResponse
的实例,将 Body
传递到 ctor 中。 ElasticsearchResponseBase
上的 ApiCall
属性有一个公共(public)获取/设置对,因此您应该能够按照以下方式执行某些操作
[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 设置的代码(删除了多余的逗号),但是如果我们想检查是否正确调用了搜索,我们应该将它们与实际参数匹配而不是使用 ()。
关于c# - 单元测试 Elasticsearch.net IElasticLowLevelClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802102/