gpt4 book ai didi

javascript - 如何测试使用 module().controller 创建的 angularjs Controller ?

转载 作者:行者123 更新时间:2023-11-30 13:02:25 25 4
gpt4 key购买 nike

为了这个问题的目的,我有一个这样定义的 Angular JS Controller :

function TestController($scope) {
$scope.name = 'eric';
}

使用我的正常 node/karma/jasmine 设置(在 phantom、chrome、canary、firefox 和 safari 中测试)测试正常:

describe('TestSquaredController', function(){
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('TestController', {$scope: scope});
}));
it('should be called eric', function() {
expect(scope.name).toEqual('eric');
});
});

但是,当我使用 angular module.controller 方法注册 Controller 时,我得到一个错误 Error: Argument 'TestController' is not a function, got undefined when using the same test.

即。

app.module('testAngularApp')
.controller('TestController', function($scope) {
$scope.test = 'eric';
});

我似乎找不到与此相关的任何内容!在我的测试中还有什么我应该初始化/做的吗?

最佳答案

您以完全相同的方式测试它们。确保在测试中通过 module function 加载带有 Controller 的模块。在 ngMock 模块中。像这样:

beforeEach(module('testAngularApp'));

关于javascript - 如何测试使用 module().controller 创建的 angularjs Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16922409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com