2016-06-08 142 views
1

茉莉花測試的新角度。我想測試,如果我定義的控制器被定義或不與開始,但我得到錯誤說Expected undefined to be defined. 這裏是我的主要代碼:茉莉花測試來測試控制器是否定義

// controller logic MatchController.js 
(function() { 
'use strict'; 

angular.module('app.match') 
     .controller('MatchController', MatchController); 


MatchController.$inject = ['APP_CONFIG', '$authUser', '$http', '$rootScope', '$state', '$stateParams', 'SearchService', 'ConfirmMatchService', 'MusicOpsService', 'ContentOpsService', 'MatchstickService', 'MatchService', 'Restangular']; 
function MatchController(APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, searchService, confirmMatchService, musicOpsService, contentOpsService, matchstickService, matchService, Restangular) { 

    var vm = this; 
. 
. 
. 
    } 
})(); 

這裏是測試文件

// MatchController.spec.js 
(function(){ 
'use strict'; 
describe('Match Controller Tests', function(){ 

    var module, MatchTestController; 

    beforeEach(function() { 
     module = angular.module('app.match'); 
    }); 

    beforeEach(inject(function ($controller) { 

     MatchTestController = $controller('MatchController', {}); 
    })); 

    describe("Match controller to be defined", function() { 

     it("should be created successfully", function() { 
      expect(MatchTestController).toBeDefined(); 
     }); 
    }); 

}); 

})(); 

我不斷收到錯誤:

TypeError: 'undefined' is not a function (evaluating 'angular.controller('MatchController')') 
undefined 
    at /Users/rgoti/match-ui/match-ui/public/src/app/match/match.controller.spec.js:16 
    at invoke (/Users/rgoti/match-ui/match-ui/public/bower_components/angular/angular.js:4219) 
    at workFn (/Users/rgoti/match-ui/match-ui/public/bower_components/angular-mocks/angular-mocks.js:2475) 
Expected undefined to be defined. 
    at /Users/rgoti/match-ui/match-ui/public/src/app/match/match.controller.spec.js:22 

不知道我在做什麼錯在這裏。

+0

你一定因果報應是要找到你的控制器js文件? –

+0

是的。我很積極地找到了這個文件 – noobcoder

回答

1

在嘲笑它之前,您應該首先在控制器中注入所有依賴項。

試試這個:

// MatchController.spec.js 
(function(){ 
'use strict'; 
describe('controller: MatchController', function(){ 

    var module, MatchController, APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, SearchService, ConfirmMatchService, MusicOpsService, ContentOpsService, MatchstickService, MatchService, Restangular; 

    beforeEach(function() { 
     module = angular.module('app.match'); 
    }); 

    beforeEach(inject(function ($controller, _APP_CONFIG_, _$authUser_, _$http_, _$rootScope_, _$state_, _$stateParams_, _SearchService_, _ConfirmMatchService_, _MusicOpsService_, _ContentOpsService_, _MatchstickService_, _MatchService_, _Restangular_) { 

     APP_CONFIG = _APP_CONFIG_; 
     $authUser = _$authUser_; 
     $http = _$http_; 
     $rootScope = _$rootScope_; 
     $state = _$state_; 
     $stateParams = _$stateParams_; 
     SearchService = _SearchService_; 
     ConfirmMatchService = _ConfirmMatchService_; 
     MusicOpsService = _MusicOpsService_; 
     ContentOpsService = _ContentOpsService_; 
     MatchstickService = _MatchstickService_; 
     MatchService = _MatchService_; 
     Restangular = _Restangular_; 

     MatchController = $controller('MatchController', { 
      APP_CONFIG: _APP_CONFIG_, 
      $authUser: _$authUser_, 
      $http: _$http_, 
      $rootScope: _$rootScope_, 
      $state: _$state_, 
      $stateParams: _$stateParams_, 
      SearchService: _SearchService_, 
      ConfirmMatchService: _ConfirmMatchService_, 
      MusicOpsService: _MusicOpsService_, 
      ContentOpsService: _ContentOpsService_, 
      MatchstickService: _MatchstickService_, 
      MatchService: _MatchService_, 
      Restangular: _Restangular_ 
     }); 


    })); 

    describe("Match controller to be defined", function() { 

     it("should be created successfully", function() { 
      expect(MatchController).toBeDefined(); 
     }); 

    }); 

}); 

})();