I'm configuring my first unit tests for my application and I've been able to test the controller, but now I'm trying to test my factory and I'm running into issues.
Here's my factory:
angular.module('app')
.factory('SS', function ($window) {
return {
set: function(key, value) {
$window.sessionStorage[key] = value;
},
get: function(key, defaultValue) {
return $window.sessionStorage[key] || defaultValue;
},
setObj: function(key, value) {
$window.sessionStorage[key] = JSON.stringify(value);
},
getObj: function(key) {
return $window.sessionStorage[key] ? JSON.parse($window.sessionStorage[key]) : null;
},
remove: function(key){
$window.sessionStorage.removeItem(key);
console.log('Session "' + key + '" has been removed.');
}
}
})
Here here's my test:
describe('App Unit Tests', function () {
var scope, ctrl, SS;
beforeEach(function () {
module('app');
inject(function (_SS_) {
SS = _SS_;
});
});
describe('Auth Controller Tests', function () {
beforeEach(function () {
inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('AuthCtrl', { $scope: scope });
})
});
describe('session storage should have null appToken', function () {
expect(SS.getObj('appToken')).toBe(null);
});
});
})
When I run the test I get an error that SS is not undefined when the test hits the expect line. I've looked up multiple guides on testing factories and as far as I can tell I'm doing this correctly. Does anyone know what might be wrong?
Aucun commentaire:
Enregistrer un commentaire