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

Error: getItem() method does not exist (Angular, mocking LocalStorage)

本文关键字:mocking LocalStorage Angular 方法 getItem 错误 不存在      更新时间:2023-09-26

我截取了一段代码来模拟本地存储,但是,当我运行测试时,它抛出了这个错误。正如它所说,它找不到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

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

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

事实证明,这只是一个简单的语法错误。再看一下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);
  });