2016-07-21 33 views
13

Tôi đang cố gắng thử nghiệm một dịch vụ đơn giản trong ứng dụng góc của tôi, sử dụng hoa nhài/karma/phantomJS.Thử nghiệm dịch vụ với góc-mocks/hoa nhài - TypeError: undefined không phải là một đối tượng

phiên bản nhài: 2.4.1 góc/góc-mocks: 1.5.7 phantomJS: 2.1.1

QueryParameters.service.tests.js: (QueryParameters.service.js là một phần của ứng dụng. mô-đun dịch vụ, và thực sự là một nhà máy , không phải là một dịch vụ)

describe('myApp.QueryParametersService', function() { 

    var QueryParametersService; 

    beforeEach(module('myApp')); 
    beforeEach(module('app.service')); 
    beforeEach(inject(function ($injector) { 
    QueryParametersService = $injector.get('QueryParametersService'); 
    })); 

    var testObject = { 
    name : 'Hans', 
    age : '27' 
    }; 

    it('Should output correct querystrings', function() { 
    expect(QueryParametersService.toQueryParams(testObject)).toBe('?name=Hans&age=27'); 
    }); 

}); 

QueryParametersService.js:

(function() { 
    'use strict'; 

    angular.module('app.service') 
    .factory('QueryParametersService', QueryParametersService); 

    QueryParametersService.$inject = []; 

    function QueryParametersService() { 

    var service = { 
     toQueryParams : toQueryParams 
    }; 

    return service; 

    function toQueryParams(queryObject) { 
     <removed code here> 
    } 
    } 
})(); 

Trong Gruntfile.js:

karma: { 
     unit: { 
     options: { 
      frameworks: ['jasmine'], 
      singleRun: true, 
      browsers: ['PhantomJS'], 
      files: [ 
      '<%= yeoman.client %>/bower_components/angular/angular.js', 
      '<%= yeoman.client %>/bower_components/angular-mocks/angular-mocks.js', 
      '<%= yeoman.client %>/app/app.js', 
      '<%= yeoman.client %>/app/filters/filter.module.js', 
      '<%= yeoman.client %>/app/directives/directive.module.js', 
      '<%= yeoman.client %>/app/services/service.module.js', 
      '<%= yeoman.client %>/app/services/tests/*.js' 
      ] 
     } 
     } 
    } 

module chính của tôi (trong app.js) được khai báo như thế này:

angular.module('myApp', [ 
    'angular-thumbnails', 
    'angularUtils.directives.dirPagination', 
    'app.directive', 
    'app.filter', 
    'app.service', 
    'color.picker', 
    'ngCookies', 
    'ngFileUpload', 
    'ngImgCrop', 
    'ngMaterial', 
    'ngMessages', 
    'ngResource', 
    'ngSanitize', 
    'ui.router', 
    'validation.match', 
    ]) 

Các im lỗi nhận được khi chạy các bài kiểm tra:

PhantomJS 2.1.1 (Mac OS X 0.0.0) myApp.QueryParametersService Should output correct querystrings FAILED 
     /<filepath>/client/bower_components/angular/angular.js:4632:53 
     [email protected]/<filepath>/client/bower_components/angular/angular.js:321:24 
     [email protected]/<filepath>/client/bower_components/angular/angular.js:4592:12 
     [email protected]/<filepath>/client/bower_components/angular/angular.js:4514:30 
     [email protected]/<filepath>/client/bower_components/angular-mocks/angular-mocks.js:3067:60 
     TypeError: undefined is not an object (evaluating 'QueryParametersService.toQueryParams') in /<filepath>/client/app/services/tests/query-parameters.service.tests.js (line 65) 
     /<filepath>/client/app/services/tests/query-parameters.service.tests.js:65:34 
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.002 secs/0.009 secs) 

Tôi đang đi sai ở đâu?

+1

Có vẻ như QueryParametersService.js không có trong tệp karma.conf.js? – jchen86

+0

Nó được bao gồm ở đây: '<% = yeoman.client%>/app/services/service.module.js', –

+0

Trên thực tế, @ jchen86, bạn đã đúng. Tôi nghĩ rằng bao gồm toàn bộ mô-đun dịch vụ sẽ là đủ, nhưng cụ thể bao gồm cả dịch vụ đã giải quyết được vấn đề của tôi. –

Trả lời

2

Như @ jchen86 đã chỉ ra, QueryParametersService.js không được bao gồm trong cấu hình (Tôi nghĩ rằng nó sẽ được bao gồm khi tôi đưa vào toàn bộ mô-đun dịch vụ). Giải quyết bằng cách thay đổi gruntfile để bao gồm tất cả các tệp js trong thư mục dịch vụ:

'<%= yeoman.client %>/app/services/*.js' 
Các vấn đề liên quan