我正在尝试围绕一些通过Nest访问Elasticsearch的代码编写一些单元测试,特别是.Search()
调用。我试图设置Moq以在调用.Search()
时返回空搜索响应:
var emptyResponse = new Nest.SearchResponse<MyDoc>()
{
Documents = new List<MyDoc>() // illegal
};
esClient.Setup(x => x.Search<MyDoc>(It.IsAny <Func<SearchDescriptor<MyDoc>, SearchDescriptor<MyDoc>>>())).Returns(emptyResponse);
不幸的是,我无法执行此操作,因为Documents
只是SearchResponse
上的<body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script>
(function (){
angular.module('exercise', [])
.directive('question', QuestionDirective)
.directive('choice', ChoiceDirective);
function QuestionDirective() {
return {
restrict: 'AE',
scope: true,
controller: function($attrs, $scope) {
$scope.question_id = $attrs['id'];
},
}
}
function ChoiceDirective() {
return {
restrict: 'AE',
scope: true,
require: '^question',
link: function(controllerInstance, element, scope, attrs) {
//this is firing!
console.log(controllerInstance.question_id);
scope.click = function(){
console.log('click')
}
}
}
}})();
</script>
<div ng-app='exercise'>
<div question id="1" status="2">
<p>
1: Question?
</p>
<button choice
ng-click="click()">choice 1</button>
</div>
</div>
</body>
。围绕搜索调用编写单元测试的建议方法是什么?
答案 0 :(得分:6)
你可能也想模仿SearchResponse
。
这里有例子:
var clientMoq = new Mock<IElasticClient>();
var searchResponseMock = new Mock<ISearchResponse<MyDoc>>();
searchResponseMock.Setup(x => x.Documents).Returns(new List<MyDoc> {new MyDoc {Id = 1}});
clientMoq.Setup(x => x.Search<MyDoc>(It.IsAny<Func<SearchDescriptor<MyDoc>, SearchDescriptor<MyDoc>>>()))
.Returns(searchResponseMock.Object);
希望它有所帮助。