I am building an application using AngularJS and I am right now developing test cases for my application. Suppose I have a service like this;
var app = angular.module('MyApp')
app.factory('SessionService', function () {
return {
get: function (key) {
return sessionStorage.getItem(key);
},
set: function (key, val) {
return sessionStorage.setItem(key, val);
},
unset: function (key) {
return sessionStorage.removeItem(key);
}
};
});
Can I write test case for my service like this;
beforeEach(module('MyApp'));
describe('Testing Service : SessionService', function (SessionService) {
var session, fetchedSession, removeSession, setSession;
beforeEach(function () {
SessionService = {
get: function (key) {
return sessionStorage.getItem(key);
},
set: function (key, val) {
return sessionStorage.setItem(key, val);
},
unset: function (key) {
return sessionStorage.removeItem(key);
}
};
spyOn(SessionService, 'get').andCallThrough();
spyOn(SessionService, 'set').andCallThrough();
spyOn(SessionService, 'unset').andCallThrough();
setSession = SessionService.set('authenticated', true);
fetchedSession = SessionService.get('authenticated');
removeSession = SessionService.unset('authenticated');
});
describe('SessionService', function () {
it('tracks that the spy was called', function () {
expect(SessionService.get).toHaveBeenCalled();
});
it('tracks all the arguments used to call the get function', function () {
expect(SessionService.get).toHaveBeenCalledWith('authenticated');
});
//Rest of the Test Cases
});
});
I am using Jasmine's spy method for developing this test case. Is it fine or am I wrong?