我有一个控制器,它向休息服务发送用户名和密码,并返回一个令牌:
.controller('Login', ['$scope', '$cookieStore', '$http', function($scope, $cookieStore, $http) {
$scope.credentials = {};
$scope.login = function(credentials){
$http.post(constants.serverAddress + 'login', credentials).success(function(data, status){
$cookieStore.put('token', data.token);
$http.defaults.headers.common['Authorization'] = 'Token ' + data.token;
});
}
}])
我正在尝试为此控制器编写单元测试。如何从单元测试中读取cookieStore的值以确保cookie已设置?
答案 0 :(得分:3)
$cookieStore
还有一个get
-method
因此在单元测试中,使用它来检索值
expect($cookieStore.get('token')).toBe('your-token');
还要确保将服务注入测试
var $cookieStore;
beforeEach(inject(function($injector) {
$cookieStore = $injector.get('$cookieStore');
}));