2015-04-30 100 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',表示應用程序文件夾內的任何腳本。感謝您的線索...... :-) –

相關問題