2016-07-21 6 views
13

मैं जैस्मीन/कर्म/प्रेतॉमजेएस का उपयोग करके अपने कोणीय ऐप में एक साधारण सेवा का परीक्षण करने की कोशिश कर रहा हूं।कोणीय-मोजे/चमेली के साथ परीक्षण सेवा - TypeError: अपरिभाषित एक वस्तु नहीं है

चमेली संस्करण: 2.4.1 कोणीय/कोणीय-mocks: 1.5.7 phantomJS: 2.1.1

QueryParameters.service.tests.js: (QueryParameters.service.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> 
    } 
    } 
})(); 
: सेवा मॉड्यूल, और वास्तव में एक कारखाने, नहीं एक सेवा)

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 है

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

अपने मुख्य मॉड्यूल (app.js में) इस तरह घोषित किया जाता है:

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', 
    ]) 

त्रुटि im जब परीक्षण चल रही:

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) 

मैं यहाँ गलत कहां जा रहा हूं?

+1

ऐसा लगता है जैसे QueryParametersService.js karma.conf.js फ़ाइलों में शामिल नहीं है? – jchen86

+0

इसमें शामिल है: '<% = yeoman.client%>/ऐप/सेवाएं/service.module.js', –

+0

दरअसल, @ jchen86, आप सही थे। मैंने सोचा कि पूरे सेवा मॉड्यूल सहित पर्याप्त होगा, लेकिन विशेष रूप से सेवा सहित मेरी समस्या हल हो गई है। –

उत्तर

2

जैसा कि @ jchen86 ने बताया, QueryParametersService.js कॉन्फ़िगरेशन में शामिल नहीं किया गया था (मैंने सोचा था कि जब मैं संपूर्ण सेवा मॉड्यूल शामिल करता हूं तो इसमें शामिल किया जाएगा)। सेवा फ़ोल्डर में सभी जेएस फ़ाइलों को शामिल करने के लिए gruntfile को बदलकर हल किया गया:

'<%= yeoman.client %>/app/services/*.js' 
संबंधित मुद्दे