2017-05-14 13 views
0

私のコントローラで私のサービスを嘲笑するのに問題があるようです。特に 'momentsService'。ここに私のテストコードがあります。ジャスミンを使ったAngularJSサービスの模擬

最後の3つのconsole.logは、 'momentsService'に対して私に未定義を与え続けます。ターミナルでは、私は '定義されていないオブジェクトではない'だから私のサービスは正しく嘲笑されていないと私のテストは失敗を続けています。

コントローラとの依存関係:

(function() { 
    angular.module('app.MomentsController', []) 

    .controller('MomentsController', ['momentsService', '$scope', '$ionicContentBanner', 'core', 'components', '$q', '$ionicPopup', MomentsController]); 

    function MomentsController (momentsService, $scope, $ionicContentBanner, core, components, $q, $ionicPopup) { 
var vm = this; 
//Omitted 

私はここで何をしないのですか?あなたの助けのための

おかげ

-Matt

EDIT: ことが重要ならば、私はcontrollerAs構文を使用しています。

EDIT2: これは、両方の修正を試みても、まだ動作しません。 momentsServiceは未定義です。次のように私のコードは、略:

describe('Moment Controller', function() { 

    var $scope, $controller, momentsService, $ionicContentBanner, core, components, $q, $ionicPopup; 

    beforeEach(module('app')); 
    beforeEach(module('core')); 
    beforeEach(module('components')); 
    beforeEach(module('constants')); 


    beforeEach(inject(function(_$controller_, $rootScope, _momentsService_, _$ionicContentBanner_, _core_, _components_, _$q_, _$ionicPopup_) { 
     var self = this; 
     this.$scope = $rootScope.$new(); 
     this.momentsService = _momentsService_; 

     this.$ionicContentBanner = _$ionicContentBanner_; 
     this.core = _core_; 
     this.components = _components_; 
     this.$q = _$q_; 
     this.$ionicPopup = _$ionicPopup_; 

     spyOn(momentsService, 'initializeView'); 

     this.$controller = _$controller_('MomentsController', { 
      momentsService: momentsService, 
      $scope: $scope, 
      $ionicContentBanner: $ionicContentBanner, 
      core: core, 
      component: components, 
      $q: $q, 
      $ionicPopup: $ionicPopup 
     }); 
    })); 

    it('should initialize', function() { 
     console.log(this); 
     console.log("CONTROLLER"); 
     console.log(this.momentsService); 
     console.log(this.momentsService.initializeView()); 
    }); 

}); 

コメントで掲示plunkerがそれを使用していませんでしたので、私もthisずに試してみました。 thisを無視してコードが正確に投稿されたプランカに一致する場合、私は他の依存関係を追加したばかりです。コメントすると、私のサービスは未定義です。理解できません。

編集3:conf.jsで

'ファイルを':

files: [ 
     'lib/ionic/js/ionic.bundle.js', 
     'lib/angular-mocks/angular-mocks.js', 
     'js/*', 
     'bestMoments/*', 
     'moments/*', 
     'submitMoment/*', 
     'myMoments/*', 
     'index.controller.js', 
     'layout/mainView.directive.js', 
    ], 

Moments.Service.jsの依存関係:

(function() { 
    angular.module('app.momentsService', []) 

    .service('momentsService', ['core', '$q', 'constants', 'awsServices', 'components', 'logger', 'geolocation', momentsService]); 

    function momentsService(core, $q, constants, awsServices, components, logger, geolocation){ 

ファイル構造:と仮定すると、 enter image description here

+1

http://jsfiddle.net/bowofola/s37266LL/2/ – Bowofola

+0

あなたplunkerあなたは私の新しいコードを確認することができ、私のために動作しませんでしたか? – MatTaNg

+0

どのモジュールがmomentsサービスで登録されていますか。 – Bowofola

答えて

1

jasmine2 - すべての行にselfが0であることを通知する。 beforeXXXXのプロパティは、thisitafterXXXXにあります。

あなたのケースでは、console.logの未定義はmomentServiceのため、itで未定義です。その問題はジャスミンの特徴によって解決される。 Official jasmine - The this Keyword

describe('Moment Controller', function() { 


    beforeEach(module('app')); 
    beforeEach(module('core')); 
    beforeEach(module('components')); 
    beforeEach(module('constants')); 


    beforeEach(inject(function(_$controller_, _$rootScope_, _momentsService_, _$ionicContentBanner_, _core_, _components_, _$q_, _$ionicPopup_) { 
     var self = this; 
     self.$scope = $rootScope.$new(); 
     var momentsService = self.momentsService = _momentsService_; 
     self.$ionicContentBanner = _$ionicContentBanner_; 
     self.core = _core_; 
     self.components = _components_; 
     self.$q = _$q_; 
     self.$ionicPopup = _$ionicPopup_; 

     spyOn(self.momentsService, 'initializeView'); 

     self.$controller = _$controller_('MomentsController', { 
      momentsService: self.momentsService, 
      $scope: self.$scope, 
      $ionicContentBanner: self.$ionicContentBanner, 
      core: self.core, 
      component: self.components, 
      $q: self.$q, 
      $ionicPopup: self.$ionicPopup 
     }); 
    })); 

    it('should initialize', function() { 
     var self = this; 
     console.log("CONTROLLER"); 
     console.log(self.momentsService); 
     console.log(self.momentsService.initializeView()); 
    }); 

}); 
+0

私の提案はうまくいかなかった。私の編集を見て、どこが間違っているのか見てみましょうか? – MatTaNg

+0

あなたの問題は 'angular.module( 'app.momentsService'、[])'を2回定義することによると思われます。秒の発生は 'angular.module( 'app.momentsService')です。すなわち、モジュールの依存関係を渡さないようにして、angleが以前に作成した' app.momentService'を返すようにします。もし、私があなたが@Bowofolaによって提案されたようにjsfiddleを作成し、問題を再現することを提案したら、助けてください。 – bhantol

+0

いくつかの調査の後、それは私のbeforeEachループをまったく打たないようです。どんな考え? – MatTaNg

関連する問題