Skip to content

Instantly share code, notes, and snippets.

@vojd
Created January 12, 2015 10:16
Show Gist options
  • Save vojd/7fb32eb74b3dec851f8f to your computer and use it in GitHub Desktop.
Save vojd/7fb32eb74b3dec851f8f to your computer and use it in GitHub Desktop.
Jasmine test example
(function(){
'use strict';
describe('Auth services test', function(){
var AuthInterceptor, AuthService, $httpProvider, $q, $window, $location;
var $httpBackend;
beforeEach( function () {
/**
* We're testing AuthInterceptor inside the ``rc.components.auth.services`` module
* It's dependent on ngCookies so we'll inject them both.
*/
module('rc.components.auth.services', 'ngCookies', function(_$httpProvider_){
$httpProvider = _$httpProvider_;
});
/**
* The service AuthInterceptor needs $q, $window and $location,
* the AuthService needs $http lets inject those as well.
*/
inject(function (_$httpBackend_, _$q_, _$window_, _$location_) {
$httpBackend = _$httpBackend_;
$q = _$q_;
$window = _$window_;
$location = _$location_;
});
/**
* With all the dependencies already injected we can now create a reference to AuthInterceptor
*/
inject(function (_AuthInterceptor_, _AuthService_) {
AuthInterceptor = _AuthInterceptor_;
AuthService = _AuthService_;
});
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
$window.sessionStorage.removeItem('auth_token');
});
describe('AuthInterceptor tests', function(){
it('should have AuthInterceptor defined', function(){
expect(AuthInterceptor).toBeDefined();
});
});
describe('AuthService tests', function () {
it('should have the AuthInterceptor in the http providers list of interceptors', function(){
expect($httpProvider.interceptors).toContain('AuthInterceptor');
});
it('should have an auth token in the headers if one exists in sessionStorage', function(){
// add an auth token to the sessionStorage
var auth_token = 'MYSECRETAUTHTOKEN';
$window.sessionStorage.setItem('auth_token', auth_token);
var config = AuthInterceptor.request({headers: {}});
expect(config.headers.Authorization).toEqual('AuthToken="'+auth_token+'"');
});
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment