1

I have a simple service implemented like this

 sameRoof
  .factory('dbService', function (localStorageService, backendUpdate) {
    return {
      checkProfileAndFlat: function () {
        return (localStorageService.get('profile') && localStorageService.get('flatshare'));
      }
    };
  });

LocalStorage are ngModules installed with bower.

I am writint unit test

'use strict';
describe('Service: service taking care of asking the local database', function () {

  var localStorageService;
  var fakeDB = {'profile' : 'testProfile', 'flatshare' : 'flatshare'};

  // load the service's module
  beforeEach(module('frontApp'));

  // instantiate service
  var dbService;
  beforeEach(inject(function (_dbService_, _localStorageService_) {
    dbService = _dbService_;
    localStorageService = _localStorageService_;

    //mock localStorageService get/add
    spyOn(localStorageService,'get').andCallFake(function(key){
      return fakeDB[key];
    });
  }));

  it('should check profile and flatshare', function () {
    console.log(localStorageService.get('profile'));
    expect( dbService.checkProfileAndFlat() ).toBe(false);
  });

});

but i am having problems here,

TypeError: 'undefined' is not a function (evaluating 'spyOn ...)

seems like i am implementing in wrong way the spyOn

Med Tumy
  • 1,705
  • 2
  • 11
  • 20

1 Answers1

0

the answer is

//mock localStorageService get/add
spyOn(localStorageService,'get').and.callFake(function(key){
  return fakeDB[key];
});

as i am using jasmine 2.3.4 and jasmine API has changed compared to the one 1.3

Med Tumy
  • 1,705
  • 2
  • 11
  • 20