gpt4 book ai didi

angularjs - jasmine angularjs 测试-参数 'PhoneListCtrl' 不是函数,未定义

转载 作者:行者123 更新时间:2023-12-04 05:50:31 24 4
gpt4 key购买 nike

运行 angularjs + Jasmine + Karma 测试时,出现以下错误:
enter image description here

我的测试脚本是:

describe('PhoneCat controllers', function() {

describe('PhoneListCtrl', function(){

it('should create "phones" model with 3 phones', inject(function($controller) {
var scope = {},
ctrl = $controller('PhoneListCtrl', { $scope: scope });

expect(scope.phones.length).toBe(3);
}));
});
});

此代码只是此处官方 AngularJS 教程的副本:
http://code.angularjs.org/1.2.0-rc.3/docs/tutorial/step_02

这是我的 karma.conf.js 文件的一部分:
// list of files / patterns to load in the browser
files: [

'js/bower_components/angular/angular.js',
'js/bower_components/angular/ngular-mocks.js',
'js/app/controllers.js',
'test/unit/*.js'
],

错误是 电话列表Ctrl 没有定义,但我相信它是在上面的代码中定义和加载的。你认为是什么问题?谢谢!

最佳答案

单元测试中缺少模块初始化部分。您应该调用 module('phonecatApp')在您第一次调用 inject() 之前.在这种情况下,您的单元测试代码应如下所示:

describe('PhoneCat controllers', function() {

describe('PhoneListCtrl', function(){

beforeEach(function() {
module('phonecatApp'); // <= initialize module that should be tested
});

it('should create "phones" model with 3 phones', inject(function($controller) {
var scope = {},
ctrl = $controller('PhoneListCtrl', { $scope: scope });

expect(scope.phones.length).toBe(3);
}));
});
});

在哪里 phonecatApp是您定义 PhoneListCtrl 的模块的名称 Controller 。

您使用的教程也已过时,它适用于不稳定版本的 Angular (1.2.0-rc.3)。这是最新版本 Angular 的同一教程的更新版本: http://docs.angularjs.org/tutorial/step_02

关于angularjs - jasmine angularjs 测试-参数 'PhoneListCtrl' 不是函数,未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23045706/

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