错误:getItem()方法不存在(Angular,模拟LocalStorage)

时间:2015-09-02 08:02:22

标签: javascript angularjs unit-testing

我抓了一段代码来模拟本地存储,但是,当我运行测试时,它抛出了这个错误。正如它所说,它无法找到getItem方法,但我在beforeEach块之外显式声明了该方法。我不知道是什么引发了这个错误 - 会很感激提示和建议!

以下是我的文件:

mainCtrl.spec.js

describe('Controller: MainCtrl', function () {
  var store = {};
  var ls = function() {
    return JSON.parse(store.storage);
  };
  var localStorage = {}; 

  var getItem = function(key) {
    return store[key];
  }

  var setItem = function(key, value) {
    store[key] = value;
  }

  beforeEach(function() {
        // setUp.
        module('mytodoApp');

        // LocalStorage mock.
        spyOn(localStorage, 'getItem').andCallFake(getItem); <-- throwing the error
        Object.defineProperty(sessionStorage, "setItem", { writable: true });
        spyOn(localStorage, 'setItem').andCallFake(setItem);
    });

  afterEach(function() {
    store = {};
  });

  var MainCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    MainCtrl = $controller('MainCtrl', {
      $scope: scope
    });
  }));

  it('should have no items to start with', function() {
    // expect(scope.todos.length).toBe(0);
    expect(Object.keys(store).length).toBe(0);
  });
});

mainCtrl.js

angular.module('mytodoApp')
  .controller('MainCtrl', function ($scope, localStorageService) {
    //  breaks on repeat or blank input
    function addTodoFn() {
        $scope.todos.push($scope.todo); 
        $scope.todo = '';   
    }

    function removeTodoFn(index) {
        $scope.todos.splice(index, 1);
    }

    function watchFn() {
      localStorageService.set('todos', $scope.todos);
    }

    //////////

    var todosInStore = localStorageService.get('todos');
    $scope.todos = todosInStore || [];
    $scope.$watch('todos', watchFn, true);
    $scope.addTodo = addTodoFn;
    $scope.removeTodo = removeTodoFn;
  });

修改

var localStorage = { 
    getItem: function(key) {
      return store[key];
    },

    setItem: function(key, value) {
      store[key] = value;
    }
  };

  beforeEach(function() {
    // setUp.
    module('mytodoApp');

    // LocalStorage mock.
    spyOn(localStorage, 'getItem').andCallFake(getItem);
    Object.defineProperty(sessionStorage, 'setItem', { writable: true });
    spyOn(localStorage, 'setItem').andCallFake(setItem);
  });

我根据@ CosmicChild的建议改变了它的建议无济于事。

新错误消息

ReferenceError: Can't find variable: getItem

2 个答案:

答案 0 :(得分:1)

您需要在您正在存根的localStorage对象中定义一个空方法,

var localStorage = {
    getItem: function(key) {
    },
    setItem: function(key, value) {
    }
};

答案 1 :(得分:0)

事实证明,这只是一个简单的语法错误。再看看Jasmine文档有帮助(也许这与你正在使用的Jasmine版本有关,但这是2.3版本中的错误语法)。

beforeEach(function() {
    // setUp.
    module('mytodoApp');

    // LocalStorage mock.
    spyOn(localStorage, 'getItem').and.callFake(getItem); <-- .and.callFake
    Object.defineProperty(sessionStorage, 'setItem', { writable: true });
    spyOn(localStorage, 'setItem').and.callFake(setItem);
  });