[英]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.