2015-04-30 69 views
8

试图为https://github.com/beeman/loopback-angular-admin设置单元测试。Angular/Karma/Jasmine:TypeError:'undefined'不是一个对象(评估'scope.awesomeThings')

应用程序/模块/约/控制器/ about.controller.js(I加入$scope.awesomeThings的东西来测试加载范围内):

'use strict'; 
angular.module('com.module.about') 
    /** 
    * @ngdoc function 
    * @name com.module.about.controller:AboutCtrl 
    * @description 
    * # AboutCtrl 
    * Controller of the clientApp 
    */ 
    .controller('AboutCtrl', function($scope) { 
    $scope.angular = angular; 
    $scope.awesomeThings = [1, 2]; 
    }); 

在客户机/测试/模块的茉莉测试/左右/控制器/ about.ctrl.js

'use strict'; 

describe('Controller: AboutCtrl', function() { 
    var AboutCtrl, 
    scope; 

    // load the controller's module 
    beforeEach(module('gettext')); 
    beforeEach(module('ui.router')); 
    beforeEach(module('com.module.about')); 

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

    it('should attach a list of awesomeThings to the scope', function() { 
    expect(scope.awesomeThings.length).toBe(3); 
    }); 
}); 

当我运行这个简单的测试,我得到:

TypeError: 'undefined' is not a function (evaluating '$rootScope.addDashboardBox(gettextCatalog.getString('About'), 'bg-maroon', 
     'ion-information', 0, 'app.about.index')') 
    at client/app/modules/about/controllers/about.config.js:6 
    at invoke (client/app/bower_components/angular/angular.js:4203) 
    at client/app/bower_components/angular/angular.js:4025 
    at forEach (client/app/bower_components/angular/angular.js:323) 
    at createInjector (client/app/bower_components/angular/angular.js:4025) 
    at workFn (client/app/bower_components/angular-mocks/angular-mocks.js:2425) 
TypeError: 'undefined' is not an object (evaluating 'scope.awesomeThings') 
    at client/test/modules/about/controllers/about.ctrl.js:21 

如果我设置日志等级:LOG_DEBUG中,大约*文件显示:

- 约/tmp/karma-debug.log

client/app/modules/about/app.about.js 
    client/app/modules/about/controllers/about.config.js 
    client/app/modules/about/controllers/about.controller.js 
    client/app/modules/about/controllers/about.routes.js 
    client/test/modules/about/controllers/about.ctrl.js 
DEBUG [web-server]: serving (cached): client/app/modules/about/app.about.js 
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.config.js 
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.controller.js 
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.routes.js 
DEBUG [web-server]: serving (cached): client/test/modules/about/controllers/about.ctrl.js 

我知道我失去了一些东西基本>的grep%,但我可以似乎没有找到什么。

+0

你是否检查它所说的事情是否未定义?是否定义? – Transcendence

+0

明确定义。如果我将它添加到视图中,它就会出现。 –

回答

7

我对初始错误没有仔细观察。实际的错误在$rootScope.addDashboardBox,这表明需要包含其他模块。

解决方案是测试脚本是:

'use strict'; 

    describe('Controller: AboutCtrl', function() { 
    var AboutCtrl, 
     scope; 

    // load the controller's module 
    beforeEach(module('ui.router')); 
    beforeEach(module('gettext')); 
    beforeEach(module('formly')); 
    beforeEach(module('angular-loading-bar')); 
    beforeEach(module('lbServices')); 
    beforeEach(module('com.module.core')); 
    beforeEach(module('com.module.settings')); 
    beforeEach(module('com.module.about')); 

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

    it('should attach a list of awesomeThings to the scope', function() { 
     expect(scope.awesomeThings.length).toBe(3); 
    }); 

    }); 
7

对于未来的我,因为它是在谷歌的第一个结果。

不要寻找外部依赖!

Karma的日志有点误导,实际的问题是主模块没有运行。例如,由Bower注入到karma.conf.jsangular-stripe需要加载实际的Stripe JS库否则会崩溃整个应用程序(这本身非常烦人)。我已将此行添加到karma.conf.js

files: [ 
    'https://js.stripe.com/v2', 

现在它可以工作。

+0

我有其他脚本不在脚本文件夹或bower_components中,我将'app/scripts/**/*。js'更改为'app/**/*。js',表示应用程序文件夹内的任何脚本。感谢您的线索...... :-) –

相关问题