angularJS:如何用spyOn模拟localStorgaeService?

时间:2015-10-07 19:51:32

标签: javascript angularjs mocking

我有一个像这样实现的简单服务

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

LocalStorage是与bower一起安装的ngModules。

我是写作单元测试

'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);
  });

});

但我在这里遇到问题,

TypeError:' undefined'不是一个功能(评估' spyOn ......)

好像我正以错误的方式实施spyOn

0 个答案:

没有答案