我似乎無法在我的控制器中嘲笑我的服務。特別是'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){
http://jsfiddle.net/bowofola/s37266LL/2/ – Bowofola
你plunker沒有工作對我來說,你可以檢查我的新代碼? – MatTaNg
什麼模塊是瞬間服務註冊。 – Bowofola