简体   繁体   English

模拟localStorage.getItem时出现错误“预期功能”

[英]Error “Function expected” when mocking localStorage.getItem

I'm trying to mock calls to localStorage.getItem, but getting error "Function expected". 我正在尝试模拟对localStorage.getItem的调用,但收到错误“预期功能”。 Here is my test code: 这是我的测试代码:

describe("AuthService Tests", function() {
 var authSvc, httpBackend, scope;

 var fakeItem = "{\"access_token\":\"giant_access_token\",\"token_type\":\"bearer\",\"expires_in\":1209599,\".issued\":\"Thu, 11 Feb 2016 13:22:45 GMT\",\".expires\":\"Thu, 25 Feb 2016 13:22:45 GMT\",\"accountType\":\"Administrator\",\"certifiedForAccess\":true}";

 var returnFakeToken = function(key) { return fakeItem; }

 beforeEach(module('app'));

 beforeEach(inject(function (_AuthService_, $q, $rootScope, $httpBackend, $state, _config_, _messages_) {
    scope = $rootScope.$new();
    spyOn(localStorage, 'getItem').and.callFake(returnFakeToken);

    authSvc = _AuthService_;

    httpBackend = $httpBackend;
 }));

 describe('Test suite 1', function () {

    it('should return true if user is Administrator', function () {

        var result = authSvc.isAdministrator(); // error here
        expect(result).toBe(true);
    });
 });
});

Service under test: 被测服务:

(function () {
'use strict';

angular
    .module('app.services')
    .factory('AuthService', AuthService);

AuthService.$inject = ['$q', '$rootScope', '$http', '$state', 'config', 'messages'];

function AuthService($q, $rootScope, $http, $state, config, messages) {
    var userIdKey = 'userId';
    var tokenKey = 'tokenKey';
    var userAuthKey = 'userAuth';

    var svc = {
        isAdministrator: isAdministrator
    };

    return svc;

    function isAdministrator() {
        var item = localStorage.getItem(tokenKey); // eror here
        var jsonparse = JSON.parse(item);
        return jsonparse.accountType == 'Administrator';
    }
})();

When I run tests, my mocked function returnFakeToken hadn't even been called and I got error (lines of code where error occured are marked with comments): 当我运行测试时,我的模拟函数returnFakeToken甚至都没有被调用,并且出现错误(发生错误的代码行带有注释):

TypeError: Function expected TypeError:预期功能

What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

You don't need to spy on localStorage . 您无需监视localStorage It is a function that is built into the browser as part of the HTML5 standard. 此功能是HTML5标准中内置于浏览器的功能。 What you should be testing is that your service returns the expected value. 您应该测试的是您的服务返回了预期值。 Also, you may need to mock out localStorage if you are testing in PhantomJS. 此外,如果要在PhantomJS中进行测试,则可能需要模拟localStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除此错误(获取错误$ localStorage.getItem不是一个函数)? - how to remove this error (getting error $localStorage.getItem is not a function)? AngularJS“localStorage.getItem”在$ resource中为null - AngularJS “localStorage.getItem” is null in $resource 错误:getItem()方法不存在(角度,嘲笑LocalStorage) - Error: getItem() method does not exist (Angular, mocking LocalStorage) Jasmin-Karma和嘲笑localStorage spyOn找不到要监视的getItem()对象 - Jasmin-Karma and mocking localStorage spyOn could not find an object to spy upon for getItem() 在Jasmine测试中模拟AngularJS $ httpBackend并使用装饰器时出现“ $ httpBackend.when不是函数”错误 - “$httpBackend.when is not a function” error when mocking AngularJS $httpBackend in Jasmine tests and using a decorator 模拟AngularJS 1服务时出现错误 - Getting error when mocking an AngularJS 1 service 如何在 angularJs 中的 localStorage getItem 中获取内部 object 值? - How to get inner object value in localStorage getItem in angularJs? 错误:期待间谍,但得到了功能 - Error: Expected a spy, but got Function JavaScript错误函数期望的对象 - JavaScript error Object expected at function 在angularjs中,当localStorage已满时,如何捕获ngStorage错误? - In angularjs how do you catch an error for ngStorage when localStorage is full?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM