最后,我得到了一个为角度控制器工作的karma测试。但是,如果我尝试使用另一个控制器执行几乎相同的测试,它将无法处理错误消息:Error: [ng:areq] Argument 'Test2Controller' is not a function, got undefined
工作测试:
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配置中注册了测试文件,但它只适用于第一个文件。没有测试环境(我指的是我的纯angular应用程序),所有的东西,每个控制器,都工作得很好。那么我到底做错了什么呢?
发布于 2015-08-20 06:59:50
我想知道错误消息是否说明了事实:没有“Test2Controller”,至少在这种情况下是这样。也许它的名字是别的什么?或者它可能位于您没有包含在karma.conf.js中的源文件中?我真的不能确定,但我猜你的测试是好的,问题是因果报应找不到你的控制器。
您可能还想在启动karma的浏览器实例中尝试调试按钮。这将打开另一个页面,您可以在chrome dev工具或firebug或您的fave JavaScript调试器中查看该页面。继续深入了解业力实际加载了什么,你可能会发现问题所在。希望这能有所帮助。
https://stackoverflow.com/questions/32096627
复制相似问题