gpt4 book ai didi

angularjs - Karma 测试似乎没有加载我的 Angularjs Controller

转载 作者:行者123 更新时间:2023-11-28 19:45:26 25 4
gpt4 key购买 nike

我用 karma 运行 Angular 测试,我的应用程序在浏览器中运行良好,但测试失败,我怀疑设置有误。

这里是 Controller 和测试:

// app/scripts/controllers/main.js

'use strict';

angular.module('GloubiBoulgaClientApp')
.controller('MainCtrl', function ($scope) {

});

这是测试文件:

'use strict';

describe('Controller: MainCtrl', function () {

// load the controller's module
beforeEach(module('GloubiBoulgaClientApp'));

var MainCtrl,
scope;

// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));

it('should attach a list of awesomeThings to the scope', function () {
expect(true).toBe(true);
});
});

karma 报应

module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',

// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],

// list of files / patterns to exclude
exclude: [],

// web server port
port: 8080,

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['PhantomJS'],


// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};

错误输出

 PhantomJS 1.9.2 (Linux) Controller: MainCtrl should attach a list of awesomeThings to the scope FAILED
Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined
http://errors.angularjs.org/1.2.8-build.2094+sha.b6c42d5/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got% 2
0undefined
at assertArg (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angula
r.js:1362)
at assertArgFn (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angu
lar.js:1373)
at --obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angular.js:6763
at --obfuscated-path--GloubiBoulga/GloubiBoulgaClient/test/spec/controllers/main.js:15
at invoke (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angular.j
s:3704)
at workFn (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular-mocks/angular -mocks.js:2120)

我想知道为什么会这样,我试图找到一些关于使用 angularjs 进行 karma 初始化的文档。但是我发现的大多数文档只是重复相同模式的虚拟教程(比如虚拟待办事项列表,但带有电话......)

$controllerProvide.register 似乎无法解析我的 Controller 名称。但是指令测试工作正常......

感谢您的关注。

编辑说明:我在此线程中将 Controller PersonCtrl 替换为 MainCtrl,因为它让人们对在哪里查看感到困惑。现在 MainCtrl 是我发现的最简单的失败示例。

此问题仅影响我的 Controller (所有 Controller ),但服务和指令的测试按预期工作

最佳答案

我解决了我的问题,我花了将近一周的时间来弄清楚为什么这不起作用。

我想提醒您,Karma Stacktrace 和错误报告,即使在 Debug模式下,也没有显示任何线索,主要是误导。我花时间在 javascript 调试器中逐帧跳转,以了解为什么我的 Controller 未加载。 (检查 Angular 的 Controller 寄存器,显示它是空的)

在我的目录中挖掘时,我发现了一个 *.js,它没有在生产中加载到索引中,而是通过测试中的 globbing 模式加载。

我移动的是我的旧 http_interceptor 服务,但没有删除文件。删除这个错误文件修复了奇怪的 Karma/Jasmine/Angular 行为。

经验教训:不要相信测试输出(但我应该相信什么?)。删除您未使用/未测试的文件。

感谢所有尝试解决此问题的人。

关于angularjs - Karma 测试似乎没有加载我的 Angularjs Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21025635/

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