2015-04-30 35 views
8

Cố gắng thiết lập đơn vị kiểm tra cho https://github.com/beeman/loopback-angular-admin.Góc/Karma/Jasmine: TypeError: 'undefined' không phải là một đối tượng (đánh giá 'scope.awesomeThings')

app/modules/về/controllers/about.controller.js (tôi đã thêm $scope.awesomeThings để tải phạm vi với một cái gì đó để kiểm tra):

'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]; 
    }); 

Các thử nghiệm hoa nhài tại client/test/modules/về/controllers/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); 
    }); 
}); 

Khi tôi chạy thử nghiệm đơn giản này, tôi nhận được:

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 

Nếu tôi đặt LogLevel: LOG_DEBUG, khoảng * file hiển thị:

->% grep về /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 

Tôi biết tôi thiếu một cái gì đó cơ bản, nhưng tôi có thể dường như không tìm thấy gì.

+0

bạn có kiểm tra xem những điều mà nó nói là không xác định được xác định hay không? – Transcendence

+0

Xác định chắc chắn. Nếu tôi thêm nó vào khung nhìn, nó sẽ xuất hiện. –

Trả lời

7

Tôi không xem xét kỹ lưỡng lỗi ban đầu. Lỗi thực tế là trong $rootScope.addDashboardBox, trong đó chỉ ra các mô-đun bổ sung cần được bao gồm.

Giải pháp dành cho những kịch bản thử nghiệm là:

'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

Trong tương lai tôi vì nó là kết quả đầu tiên tại Google.

Hãy tìm phụ thuộc bên ngoài!

Nhật ký của Karma hơi gây hiểu lầm, vấn đề thực tế là mô-đun chính không chạy. Ví dụ: angular-stripe, được tiêm vào karma.conf.js bởi Bower, yêu cầu thư viện Stripe JS thực sự được tải nếu không làm hỏng toàn bộ ứng dụng (chính bản thân nó rất khó chịu). Tôi đã thêm dòng này vào karma.conf.js:

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

và bây giờ nó hoạt động.

+0

Tôi đã có các tập lệnh bổ sung không có trong thư mục tập lệnh hoặc bower_components, tôi đã thay đổi 'app/scripts/**/*. Js' thành 'app/**/*. Js', có nghĩa là bất kỳ tập lệnh nào bên trong thư mục ứng dụng. Cảm ơn cho các đầu mối ... :-) –

Các vấn đề liên quan