我的问题与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
)?
答案 0 :(得分: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});
}));