首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一个Angular Controller Karma-Jasmine测试有效,但另一个不行

一个Angular Controller Karma-Jasmine测试有效,但另一个不行
EN

Stack Overflow用户
提问于 2015-08-19 21:16:48
回答 1查看 167关注 0票数 0

最后,我得到了一个为角度控制器工作的karma测试。但是,如果我尝试使用另一个控制器执行几乎相同的测试,它将无法处理错误消息:Error: [ng:areq] Argument 'Test2Controller' is not a function, got undefined

工作测试:

代码语言:javascript
复制
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');
    })
})
});

这里一切都很好。但这一条不是:

代码语言:javascript
复制
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应用程序),所有的东西,每个控制器,都工作得很好。那么我到底做错了什么呢?

EN

回答 1

Stack Overflow用户

发布于 2015-08-20 06:59:50

我想知道错误消息是否说明了事实:没有“Test2Controller”,至少在这种情况下是这样。也许它的名字是别的什么?或者它可能位于您没有包含在karma.conf.js中的源文件中?我真的不能确定,但我猜你的测试是好的,问题是因果报应找不到你的控制器。

您可能还想在启动karma的浏览器实例中尝试调试按钮。这将打开另一个页面,您可以在chrome dev工具或firebug或您的fave JavaScript调试器中查看该页面。继续深入了解业力实际加载了什么,你可能会发现问题所在。希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32096627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档