2017-05-14 106 views
0

我似乎無法在我的控制器中嘲笑我的服務。特別是'momentsService'。這是我的測試代碼。茉莉花嘲笑AngularJS服務

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_) { 
     $scope = $rootScope.$new(); 
     momentsService = _momentsService_; 
     $ionicContentBanner = _$ionicContentBanner_; 
     core = _core_; 
     components = _components_; 
     $q = _$q_; 
     $ionicPopup = _$ionicPopup_; 

     spyOn(momentsService, 'initializeView'); 

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

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

}); 

最後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

編輯: 我使用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()); 
    }); 

}); 

我已經沒有this嘗試,以及因爲張貼在評論中plunker沒有使用它。如果你忽視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

什麼模塊是瞬間服務註冊。 – Bowofola

回答

1

假設jasmine2 - 注意與self這是0的所有行。 beforeXXXX中的任何屬性可在thisitafterXXXX中獲得。

對於您的情況,未定義的是在console.log上是對於未在it中定義的momentService。茉莉花功能解決了這個問題。 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',[])'兩次。秒的出現應該是'angular.module('app.momentsService')'即不傳遞模塊依賴關係,這樣angular會返回之前創建的'app.momentService'。如果這種做法幫助我建議你按照@Bowofola的建議創建jsfiddle並重新創建問題? – bhantol

+0

經過一番調查,似乎它沒有擊中我的beforeEach循環。任何想法爲什麼? – MatTaNg