After successfully setting up a karma test for an Angular controller, I encountered an issue when trying to run a similar test with another controller. The error message received was:
Error: [ng:areq] Argument 'Test2Controller' is not a function, got undefined
The first test that worked:
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');
})
})
});
However, the second test did not work as expected:
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');
})
})
});
I have registered the test files in the karma config, but only the first one seems to be working properly. In my regular Angular app environment, all controllers function correctly. What could be causing this discrepancy?