我的注销功能向注销 api 发送请求并清除所有 cookie,从而有效地结束会话。它工作得很好,但是当我在测试中运行该函数时,我得到:
ReferenceError: $ is not defined
我知道这是从什么时候开始的,在注销功能中,我尝试通过运行来清除所有 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()
}));
});