最後我得到了角度控制器的一個業力測試。但如果我嘗試用另一個控制器執行幾乎同樣的測試,它無法與錯誤信息的工作:Error: [ng:areq] Argument 'Test2Controller' is not a function, got undefined
一個角度控制器Karma-Jasmine測試工作,但不是另一個
工作測試:
describe('TestController: - ', function() {
beforeEach(module('myApp'));
var scope, $controller, httpBackend;
beforeEach(inject(function ($rootScope, _$controller_, $httpBackend) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
$controller = _$controller_;
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
describe('version testing; -', function() {
it("tests the version ", function() {
httpBackend.whenGET('url').respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}});
var $scope = {};
var controller = $controller('TestController', { $scope: $scope });
httpBackend.flush();
expect($scope.version.meta.apiVersion).toEqual('0.1');
expect($scope.version1).toEqual('1');
})
})
});
萬物工作在這裏很好。但是這個沒有:
describe('Test2Controller: - ', function() {
beforeEach(module('myApp'));
var scope, $controller, httpBackend;
beforeEach(inject(function ($rootScope, _$controller_, $httpBackend) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
$controller = _$controller_;
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
describe('test 2 testing; -', function() {
it("tests the test2 ", function() {
httpBackend.whenGET('url').respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}});
var $scope = {};
var controller = $controller('Test2Controller', { $scope: $scope });
httpBackend.flush();
expect($scope.testVal).toEqual('Test Value');
})
})
});
我也在karma配置中註冊了測試文件,但它只是爲第一個配置工作。沒有測試環境(我的意思是我的純角度應用程序),每個控制器都工作正常。那麼我做錯了什麼?