2016-07-21 15 views
13

角度アプリで単純なサービスをテストしようとしていますが、jasmine/karma/phantomJSを使用しています。角度モック/ジャスミンでのテストサービス - TypeError:未定義はオブジェクトではありません

ジャスミンバージョン:2.4.1 角度/角速度-モック:1.5.7 phantomJS:2.1.1

QueryParameters.service.tests.js:(QueryParameters.service.jsアプリの一部です。サービスモジュール、および実際工場、ないサービスである)

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

テストを実行しているときにエラーイムが取得:

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

karma.conf.jsファイルにQueryParametersService.jsが含まれていないようですか? – jchen86

+0

それはここに含まれています: '<%= yeoman.client%>/app/services/service.module.js'、 –

+0

実際、@ jchen86、あなたは正しいです。私はサービスモジュール全体を含めて十分であると思ったが、具体的には私の問題を解決したサービスを含めた。 –

答えて

2

@ jchen86が指摘したように、QueryParametersService.jsは構成に含まれていませんでした(サービスモジュール全体を含めると、それが含まれると思いました)。サービスフォルダ内のすべてのjsファイルを含むようにgruntfileを変更して解決します。

'<%= yeoman.client %>/app/services/*.js' 
関連する問題