2013-07-10 134 views
3

我的問題與Testing Angular Controllers defined like angular.module('myApp').controller(非常相似。我並不想劫持這個問題,而是想單獨問我的問題。當我使用表格的建議答案時:測試角度控制器:未定義控制器

describe('evCalcApp controllers', function(){ 
    beforeEach(module('evCalcApp.controllers')); 
    var scope, ctrl 
    beforeEach(inject(function($controller, $rootScope) { 
     scope = $rootScope.$new(); 
     ctrl = $controller('MyMileageCalcController', {$scope: scope}); 
    })); 

這對第一個控制器工作正常。但是,如果您在同一個文件中測試了多個控制器,那麼您將如何注入第二個控制器(讓我們稱它爲MyCtrl2)?

回答

3

相同的方式,做了最後一個

describe('evCalcApp controllers', function(){ 
    beforeEach(module('evCalcApp.controllers')); 
    var scope, ctrl, ctrl2; 
    beforeEach(inject(function($controller, $rootScope) { 
     scope = $rootScope.$new(); 
     scope2 = $rootScope.$new(); 
     ctrl = $controller('MyMileageCalcController', {$scope: scope}); 
     ctrl2 = $controller('MyCtrl2', {$scope: scope2}); 
    })); 
+0

太好了!謝謝。所以看起來這不能進入給定控制器的'describe'塊,而是必須存在於模塊的外部'describe'內。對? –