我在控制器中有这个功能:
$scope.delt = function() {
$scope.data = {};
$scope.confirmPopup = $ionicPopup.confirm({
title: '<b>Delete user</b>',
template: "Are you sure you want to delete this user ?<br>can't be undo."
}).then(function(res) {
if (res) {
API.editeTheUser.delete({ id: $scope.user.id }, function(res, header) {
$scope.addEvent('delete-user', 'Delete the user with phone_number :' + $scope.user.phone);
$rootScope.popup('delete', "delete was success");
$ionicHistory.goBack();
}, function(err) {
$rootScope.popup("Error", err.data.error);
});
} else {
console.log('You are not sure');
}
});
}
当我在单元测试中调用此函数时:
describe('manageUserCtrl', function() {
var controller, window, scope,
$rootScope,
$q, store, API, $ionicPopup, deferredLogup;
beforeEach(inject(function($controller, _$ionicPopup_, _$rootScope_, $q, _API_, _$window_) {
$q = $q;;
$ionicPopup = _$ionicPopup_;
deferredLogup = $q.defer();
$rootScope = _$rootScope_;
spyOn($ionicPopup, 'confirm');
scope = $rootScope.$new();
API = _API_;
window = _$window_;
controller = $controller('manageUserCtrl', {
'$scope': scope,
'API': API,
'$window': window,
'$ionicPopup': $ionicPopup
});
}));
it('expect delete', function() {
scope.delt();
});
});
然后我收到了错误
“TypeError:undefined不是构造函数(near '...})。然后(函数(RES))({...')“
。 这里的错误是什么,我是单元测试的新手? 附:代码很好用。
答案 0 :(得分:0)
描述函数结束时的错字 - 您没有关闭父级别(描述函数) - 请注意我添加了注释仅用于描述目的。
describe('manageUserCtrl', function() {
var controller, window, scope,
$rootScope,$q, store, API, $ionicPopup, deferredLogup;
beforeEach(inject(function($controller, _$ionicPopup_, _$rootScope_, $q, _API_, _$window_) {
$q = $q;;
$ionicPopup = _$ionicPopup_;
deferredLogup = $q.defer();
$rootScope = _$rootScope_;
spyOn($ionicPopup, 'confirm');
scope = $rootScope.$new();
API = _API_;
window = _$window_;
controller = $controller('manageUserCtrl', {
'$scope': scope,
'API': API,
'$window': window,
'$ionicPopup': $ionicPopup
}); // closes the controller
})); //closes the beforeEachfuntion
it('expect delete', function() {
scope.delt();
}); //closes the expect rule
}); //closes the parent describe function