Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs “错误”;“预期功能”;模拟localStorage.getItem时_Angularjs_Unit Testing_Mocking_Jasmine - Fatal编程技术网

Angularjs “错误”;“预期功能”;模拟localStorage.getItem时

Angularjs “错误”;“预期功能”;模拟localStorage.getItem时,angularjs,unit-testing,mocking,jasmine,Angularjs,Unit Testing,Mocking,Jasmine,我试图模拟对localStorage.getItem的调用,但得到错误“预期函数”。以下是我的测试代码: describe("AuthService Tests", function() { var authSvc, httpBackend, scope; var fakeItem = "{\"access_token\":\"giant_access_token\",\"token_type\":\"bearer\",\"expires_in\":1209599,\".issued\":

我试图模拟对localStorage.getItem的调用,但得到错误“预期函数”。以下是我的测试代码:

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);
    });
 });
});
测试中的服务:

(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';
    }
})();
当我运行测试时,我的模拟函数returnFakeToken甚至没有被调用,我得到了错误(发生错误的代码行用注释标记):

TypeError:需要函数

我做错了什么


谢谢

您不需要监视
localStorage
。它是作为HTML5标准的一部分内置到浏览器中的一个功能。您应该测试的是您的服务是否返回预期值。此外,如果您在PhantomJS中进行测试,您可能需要模拟
localStorage

实际上,我不是在浏览器中运行测试,而是在cordva应用程序中运行测试,但是,是的,您是对的,没有必要模拟localStorage。非常感谢。科尔多瓦正在为你模拟一个浏览器。很酷,它能做到,嗯?