对测试 $promise 感到困惑
confused about testing $promise
我正在测试一个控制器,它有一个方法 returns 来自我的服务器的 json 对象。我真的很困惑如何对我的方法返回的内容做出期望。
我需要根据结果清理范围。
我想要完成的是调用 search() -> 查看返回的数据 -> 如果长度==0 -> 确保我的范围已删除某些值。我如何检查我的方法返回了什么?
$scope.clearIds = function(){
//clean out data I don't need anymore
}
$scope.search = function(inputValue, modelname, action, field){
modelname = modelname || 'companies';
action = action || 'search';
field = field || 'title';
return apiResource.query({api_resource:modelname, api_action:action, api_column:field, api_value:inputValue}).$promise.then(function(response){
if(response.data.length === 0){
$scope.clearIds();
}
else{
return response.data;
}
});
}
describe('Controller - TypeaheadSearch', function () {
// load the controller's module
beforeEach(module('app'));
var scope;
var apiResource;
var stateParams;
var q;
var deferred;
var rootScope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $q) {
apiResource = {
query: function() {
deferred = $q.defer();
deferred.resolve('bar');
return deferred.promise;
}
};
scope = $rootScope.$new();
stateParams = {};
q = $q;
rootScope = $rootScope;
$controller('TypeaheadSearch', {
$scope: scope,
$stateParams:stateParams,
apiResource: apiResource,
});
}));
it('Should call apiResource:query', function() {
spyOn(apiResource, 'query').and.callThrough();
scope.search();
rootScope.$apply();
expect(apiResource.query).toHaveBeenCalled();
});
});
为 $scope.clearIds() 创建间谍,然后 expect(scope.clearIds).toHaveBeenCalled()
、and/or 检查 clearIds()
是否达到了您想要的效果。
接下来,使用类似于您的代码预期的数据解决虚假承诺。
在你的 (it()
) 测试中这样做,这样你就可以看到代码如何响应不同的数据(例如返回一个包含某些内容的数组)。
deferred.resolve({data: []});
我正在测试一个控制器,它有一个方法 returns 来自我的服务器的 json 对象。我真的很困惑如何对我的方法返回的内容做出期望。
我需要根据结果清理范围。
我想要完成的是调用 search() -> 查看返回的数据 -> 如果长度==0 -> 确保我的范围已删除某些值。我如何检查我的方法返回了什么?
$scope.clearIds = function(){
//clean out data I don't need anymore
}
$scope.search = function(inputValue, modelname, action, field){
modelname = modelname || 'companies';
action = action || 'search';
field = field || 'title';
return apiResource.query({api_resource:modelname, api_action:action, api_column:field, api_value:inputValue}).$promise.then(function(response){
if(response.data.length === 0){
$scope.clearIds();
}
else{
return response.data;
}
});
}
describe('Controller - TypeaheadSearch', function () {
// load the controller's module
beforeEach(module('app'));
var scope;
var apiResource;
var stateParams;
var q;
var deferred;
var rootScope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $q) {
apiResource = {
query: function() {
deferred = $q.defer();
deferred.resolve('bar');
return deferred.promise;
}
};
scope = $rootScope.$new();
stateParams = {};
q = $q;
rootScope = $rootScope;
$controller('TypeaheadSearch', {
$scope: scope,
$stateParams:stateParams,
apiResource: apiResource,
});
}));
it('Should call apiResource:query', function() {
spyOn(apiResource, 'query').and.callThrough();
scope.search();
rootScope.$apply();
expect(apiResource.query).toHaveBeenCalled();
});
});
为 $scope.clearIds() 创建间谍,然后 expect(scope.clearIds).toHaveBeenCalled()
、and/or 检查 clearIds()
是否达到了您想要的效果。
接下来,使用类似于您的代码预期的数据解决虚假承诺。
在你的 (it()
) 测试中这样做,这样你就可以看到代码如何响应不同的数据(例如返回一个包含某些内容的数组)。
deferred.resolve({data: []});