Angular 业力测试 $.cookie
Angular Karma Testing $.cookie
我的注销功能向注销 api 发送请求并清除所有有效结束会话的 cookie。它工作得很好但是当我 运行 测试中的函数时,我得到:
ReferenceError: $ is not defined
我知道这是因为在注销功能中,我尝试通过 运行ning:
清除我所有的 cookie
for(var cookie in $.cookie()) {
$.removeCookie(cookie);
}
感谢观看!其余代码如下:
控制器
simulatorApp.controller('homeController', function homeController($scope, $http, $cookies, localAPI) {
$scope.logout = function() {
var logoutRequest = {
method: 'POST',
url: localAPI.url + 'auth/logout/',
headers: requestHeaders
};
$http(logoutRequest);
for(var cookie in $.cookie()) {
$.removeCookie(cookie);
}
};
测试
describe('homeController', function() {
beforeEach(module('simulatorApp'));
var controller;
beforeEach(inject(function(_$controller_) {
controller = _$controller_;
}));
var $scope = {};
beforeEach(inject(function($controller, $httpBackend) {
$httpBackend.whenPOST("http://0.0.0.0:8000/api/logout/").respond("Good for you");
controller('homeController', {
$scope: $scope
});
$scope.logout();
$httpBackend.flush()
}));
});
$ 是 JQuery 的关键字,所以这里的问题是你的业力运行时不知道 $ 是什么。
在 karma.conf.js
的文件部分添加您的 jquery 源可以解决这个问题。
我的注销功能向注销 api 发送请求并清除所有有效结束会话的 cookie。它工作得很好但是当我 运行 测试中的函数时,我得到:
ReferenceError: $ is not defined
我知道这是因为在注销功能中,我尝试通过 运行ning:
清除我所有的 cookiefor(var cookie in $.cookie()) {
$.removeCookie(cookie);
}
感谢观看!其余代码如下:
控制器
simulatorApp.controller('homeController', function homeController($scope, $http, $cookies, localAPI) {
$scope.logout = function() {
var logoutRequest = {
method: 'POST',
url: localAPI.url + 'auth/logout/',
headers: requestHeaders
};
$http(logoutRequest);
for(var cookie in $.cookie()) {
$.removeCookie(cookie);
}
};
测试
describe('homeController', function() {
beforeEach(module('simulatorApp'));
var controller;
beforeEach(inject(function(_$controller_) {
controller = _$controller_;
}));
var $scope = {};
beforeEach(inject(function($controller, $httpBackend) {
$httpBackend.whenPOST("http://0.0.0.0:8000/api/logout/").respond("Good for you");
controller('homeController', {
$scope: $scope
});
$scope.logout();
$httpBackend.flush()
}));
});
$ 是 JQuery 的关键字,所以这里的问题是你的业力运行时不知道 $ 是什么。
在 karma.conf.js
的文件部分添加您的 jquery 源可以解决这个问题。