一个Angular Controller Karma-Jasmine测试可以工作,但另一个不行

One Angular Controller Karma-Jasmine test works, but not another one

本文关键字:另一个 工作 Controller Angular Karma-Jasmine 测试 一个      更新时间:2023-09-26

最后我得到了一个业力测试工作的角度控制器。但是,如果我尝试使用另一个控制器执行几乎相同的测试,则无法使用错误信息: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应用程序),所有东西,每个控制器,都工作得很好。我做错了什么?

我想知道错误信息是否属实:至少在这种情况下,没有'Test2Controller'。也许它有别的名字?或者它存在于一个源文件中,而没有包含在karma.conf.js中?不能确定,但我猜你的测试很好,问题是因果报应找不到你的控制器。

您可能还想在浏览器实例karma启动时尝试调试按钮。这打开了另一个页面,你可以在chrome开发工具或firebug或你最喜欢的JavaScript调试器中检查。继续深入研究,看看业力实际上装载了什么,你可能会发现问题所在。

相关文章: