Javascript 因果报应错误-预期未定义

Javascript 因果报应错误-预期未定义,javascript,angularjs,karma-runner,karma-jasmine,spyon,Javascript,Angularjs,Karma Runner,Karma Jasmine,Spyon,我想对我的控制器进行单元测试。我从ExpectAPI的基本测试断言开始。但我在条件检查中模拟作用域方法时面临挑战。我收到一个未定义的错误,因为它在作用域下不可用,只有全局注销()方法可用 我尝试使用spyOn将localStorageService模拟为true以满足条件,但仍然没有任何帮助。任何解决办法都会对我有很大帮助 控制器: angular.module('app').controller('sampleCtrl', function($scope, $state, $

我想对我的控制器进行单元测试。我从ExpectAPI的基本测试断言开始。但我在条件检查中模拟作用域方法时面临挑战。我收到一个
未定义的
错误,因为它在作用域下不可用,只有全局
注销()方法可用

我尝试使用
spyOn
localStorageService
模拟为true以满足条件,但仍然没有任何帮助。任何解决办法都会对我有很大帮助

控制器:

angular.module('app').controller('sampleCtrl',

        function($scope, $state, $http, $rootScope, localStorageService) {

            if (!(localStorageService.get('isAuthenticated'))) {

                 $state.go('home');

            }
            if (localStorageService.get('isAuthenticated') === true) {

                 //http post calls made here to perform certain operation on page load

                 $scope.someMethod = function(){

                     //do something

                  }

            }

            $scope.logOut = function() {

               localStorageService.set('property', '');

               localStorageService.set('isAuthenticated', false);

               $state.go('home');

          };
 });
业力:

'use strict';

describe('Controller: sampleCtrl', function() {

    /** to load the controller's module */
    beforeEach(module('app'));

    var sampleCtrl,scope,httpBackend,deferred,rootScope;

    beforeEach(inject(function ($controller,_$rootScope_,$httpBackend,$q) {

        var store = {};
        scope= _$rootScope_.$new(); // creates a new child scope of $rootScope for each test case
        rootScope           = _$rootScope_;
        localStorageService = _localStorageService_;
        httpBackend         = $httpBackend;

        httpBackend.whenGET(/\.html$/).respond(''); 

        spyOn(localStorageService, 'set').and.callFake(function (key,val) {
            store[key]=val;
         });

        spyOn(localStorageService, 'get').and.callFake(function(key) {
            return store[key];
         });

        sampleCtrl = $controller('sampleCtrl',{
            _$rootScope_:rootScope,
             $scope:scope,
             $httpBackend:httpBackend,
            _localStorageService_:localStorageService
            // add mocks here
        });

        localStorageService.set('isAuthenticated',true);

    }));

    /**ensures $httpBackend doesn’t have any outstanding expectations or requests after each test*/
    afterEach(function() {
        httpBackend.verifyNoOutstandingExpectation(); 
        httpBackend.verifyNoOutstandingRequest();     
    }); 


    it('sampleCtrl to be defined:',function(){

        httpBackend.flush(); 
        expect(sampleCtrl).toBeDefined();

    });

    // failing test case - scope.someMethod not available in scope
    it('is to ensure only authenticated user can access the state methods',function(){
            localStorageService.get('isAuthenticated');
            httpBackend.flush();
            expect(scope.someMethod).toBeDefined(); 
    });


});

我已经设法让它工作了。
问题是localStorageService在启动控制器时未将isAuthenticated设置为true。在调用控制器之前将其设置为true

你为什么不在if之外定义一些方法,并传递它工作所需的参数呢?@min che:如果我错了,请纠正我。。我的意图是仅为经过身份验证的用户执行该方法。。如果我为了测试而把它移出状态,它不是在违抗它吗?还有…我正在努力理解它为什么不起作用,以及如何使它起作用..而不是为它寻找解决办法..非常感谢..我甚至没有想到这些话..:-)它解决了那个问题,但它开启了一个新的问题。。因为我在启动控制器之前将其放置在beforeach()块中。。它会触发控制器中的所有http服务调用,即使对于我只想断言是否定义了作用域方法的测试用例也是如此。我已修改控制器以显示http调用的位置。。因此。。。我试图断言特定功能的所有单元测试用例都受到影响,迫使我在每个测试用例中解析/模拟http调用