与噶角测试:"模块没有定义" [英] Angular testing with Karma: "module is not defined"
问题描述
我知道这个问题已经被问了很多次,我知道,在大多数情况下,人们缺少角mocks.js
文件。
我遇到了同样的问题,试图在模块上测试工厂。不幸的是,我一直运行到的问题与测试(为什么,角,哦,为什么你必须承担起窗口
和文件
反对呢?),其中指出模块没有定义。我不知所措。我使用angular.mocks.module也尝试过,但后来我得到一个消息,说是没有定义的角。我在做什么错了?
值得注意的是,我使用一饮而尽作为任务亚军。我gulpfile(尚不完善,任务不链接):
VAR一饮而尽=要求('吞掉'), 丑化=要求('咕嘟咕嘟-丑化),
jshint =要求('一饮而尽-jshint'),
茉莉=要求('一饮而尽-茉莉'),
业力=要求('一饮而尽 - 卡玛), 路径= {
剧本:剧本/ * JS
规格:投机/ * JS
DIST:DIST
};gulp.task('prepare',函数(){
返回gulp.src(paths.scripts)
.pipe(jshint())
.pipe(jshint.reporter(默认))
.pipe(丑化())
.pipe(gulp.dest(paths.dist))
});gulp.task(测试,函数(){
gulp.src([paths.scripts,paths.spec])
.pipe(因果报应({
CONFIGFILE:karma.conf.js',
动作:'运行'
}));
});gulp.task('默认',['$ P $ ppare','测试']);
我karma.conf.js,业力的init产生:
//噶配置
//生成于星期五2014年3月14日14时24分三十秒GMT-0400(EDT)module.exports =功能(配置){
config.set({ 将用于解决所有的模式//基本路径(如文件,排除)
基本路径:'',
//框架使用
//可用的框架:https://npmjs.org/browse/keyword/karma-adapter
框架:['茉莉'],
//的文件/目录模式在浏览器中加载
文件:
./lib/angular/angular.min.js',
./lib/angular-mocks/angular-mocks.js',
./src/*.js',
./spec/*.js
]
//文件的列表中排除
排除: ]
// preprocess他们服务到浏览器之前匹配的文件
//用preprocessors:https://npmjs.org/browse/keyword/karma-$p$pprocessor
preprocessors:{ },
//测试结果记者使用
//可能的值:点,进步
//可用记者:https://npmjs.org/browse/keyword/karma-reporter
记者:进步]
// Web服务器端口
端口:9876,
//使输出/禁止的颜色(记者和日志)
颜色:真,
//日志记录级别
//可能的值:config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
LOGLEVEL:config.LOG_INFO,
//启用/禁用看文件和执行测试,只要任何文件更改
autoWatch:假的,
//启动这些浏览器
//浏览器可发射:https://npmjs.org/browse/keyword/karma-launcher
浏览器:['铬'],
//持续集成模式
//如果为真,噶捕捉浏览器,运行测试,退出
singleRun:假的
});
};
最后,我的测试套件(没有的成立是,如果我可以通过这个障碍,我们会好):
/ *试验memento.js。 * /描述(纪念核心测试套件',函数(){
VAR纪念品; beforeEach(模块('备忘录')); beforeEach(函数(){
注(功能(_memento_){
纪念= _memento_;
});
}); //检查功能。
//检查,看看是否有预期的功能
它('应该与预期接口',函数(){
期待(angular.isFunction(memento.canUndo))TOBE(真)。
期待(angular.isFunction(memento.canRedo))TOBE(真)。
期待(angular.isFunction(memento.undo))TOBE(真)。
期待(angular.isFunction(memento.redo))TOBE(真)。
期待(angular.isFunction(memento.push))TOBE(真)。
}); 它('应该初始化',函数(){
this.fail(错误(测试未实现'));
}); 它('应推动()改变的对象,函数(){
this.fail(错误(测试未实现'));
}); 它('不应该推()不变的对象,函数(){
this.fail(错误(测试未实现'));
}); 它('应在撤销原返回对象(),()的函数{
this.fail(错误(测试未实现'));
}); 它('应该返回修改的对象上重做()',函数(){
this.fail(错误(测试未实现'));
}); 它('不应该撤消()如果在堆栈的开始',函数(){
this.fail(错误(测试未实现'));
}); 它('不应该重做()如果在栈底',函数(){
this.fail(错误(测试未实现'));
}); // TODO:实现恢复原来,清除历史记录。
//
//它('应该还原返回种子对象(),()的函数{
// this.fail(错误(测试未实现'));
//}); //它('应该清楚明确()栈',函数(){
// this.fail(错误(测试未实现'));
//});
});
有谁看到什么出错?我不知道是否有东西真的很明显我错过了 - 我可以用一组额外的眼睛,还是很多的。我本来以为我能够运行这是一个简单的茉莉花测试套件没有噶,但由于棱角分明,有问题。如果我不能得到这个工作,我可能只是用故宫的角包,并改变我的角模块,这样就支持CommonJS的...
谢谢大家!希望我是不是疯了。
编辑:我包括我的devdependencies
devDependencies:{
吞掉:〜3.5.6
咕嘟咕嘟-丑化:〜0.2.1
吞掉-jshint:〜1.5.0
吞掉-茉莉花:〜0.2.0
角度:〜1.2.10,
报应:〜0.12.0
吞掉-报应:0.0.4
卡玛 - 茉莉花:〜0.2.2
报应 - 铬 - 发射:〜0.1.2
}
该消息指出模块/角没有定义意味着你的角mocks.js文件不被加载,尽管你拥有了它在所列的事实,你的karma.conf.js文件。
您遇到的问题是一饮而尽 - 卡玛忽略你的karma.conf.js文件数组。当你通过一个字符串或水珠进入gulp.src在gulpfile发生这种情况。
要解决这个问题,通过gulp.src一个字符串虚假文件,./foobar来说,这将导致文件数组中的karma.conf.js文件来代替。
gulp.task(测试,函数(){
gulp.src('./ FOOBAR')
.pipe(因果报应({
CONFIGFILE:karma.conf.js',
动作:'运行'
}));
});
希望这有助于!
参考:
<一href=\"https://github.com/lazd/gulp-karma/issues/9\">https://github.com/lazd/gulp-karma/issues/9
I know this question has been asked many times, and I know that in most cases people are missing the angular-mocks.js
file.
I'm running into the same issue, attempting to test a factory on a module. Unfortunately, I keep running into issues with the tests (why, Angular, oh why must you assume a window
and document
object?), which state that module is not defined. I'm at a loss. I've also tried using angular.mocks.module, but then I get a message saying Angular isn't defined. What am I doing wrong?
Of note, I'm using gulp as a task runner. My gulpfile (not yet perfect, tasks aren't linked):
var gulp = require('gulp'),
uglify = require('gulp-uglify'),
jshint = require('gulp-jshint'),
jasmine = require('gulp-jasmine'),
karma = require('gulp-karma'),
paths = {
scripts: "scripts/*.js",
spec: "spec/*.js",
dist: "dist"
};
gulp.task('prepare', function () {
return gulp.src(paths.scripts)
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(uglify())
.pipe(gulp.dest(paths.dist))
});
gulp.task('test', function () {
gulp.src([paths.scripts, paths.spec])
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}));
});
gulp.task('default', ['prepare', 'test']);
My karma.conf.js, generated by karma init:
// Karma configuration
// Generated on Fri Mar 14 2014 14:24:30 GMT-0400 (EDT)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'./lib/angular/angular.min.js',
'./lib/angular-mocks/angular-mocks.js',
'./src/*.js',
'./spec/*.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
And finally, my test suite (nothing's set up yet, if I can pass this hurdle, we'll be good):
/* Tests for memento.js. */
describe('memento core test suite', function () {
var memento;
beforeEach(module('Memento'));
beforeEach(function() {
inject(function(_memento_) {
memento = _memento_;
});
});
// Check functions.
// check to see if it has the expected function
it('should match expected interface', function () {
expect(angular.isFunction(memento.canUndo)).toBe(true);
expect(angular.isFunction(memento.canRedo)).toBe(true);
expect(angular.isFunction(memento.undo)).toBe(true);
expect(angular.isFunction(memento.redo)).toBe(true);
expect(angular.isFunction(memento.push)).toBe(true);
});
it('should initialize', function () {
this.fail(Error('Test not implemented'));
});
it('should push() a changed object', function () {
this.fail(Error('Test not implemented'));
});
it('should not push() an unchanged object', function () {
this.fail(Error('Test not implemented'));
});
it('should return original object on undo()', function () {
this.fail(Error('Test not implemented'));
});
it('should return modified object on redo()', function () {
this.fail(Error('Test not implemented'));
});
it('should not undo() if at beginning of stack', function () {
this.fail(Error('Test not implemented'));
});
it('should not redo() if at end of stack', function () {
this.fail(Error('Test not implemented'));
});
// TODO: Implement revert to original, clearing history.
//
// it('should return seed object on revert()', function () {
// this.fail(Error('Test not implemented'));
// });
// it('should clear the stack on clear()', function () {
// this.fail(Error('Test not implemented'));
// });
});
Does anyone see anything awry? I'm not sure if there's something really obvious I'm missing - I could use an extra set of eyes, or many. I originally thought I'd be able to run this as a simple Jasmine test suite without Karma, but due to Angular, that has problems. If I can't get this to work, I might just use npm's Angular package and alter my Angular module so that it supports CommonJS...
Thanks, everyone! Hope I'm not crazy.
Edit: I've included my devdependencies.
"devDependencies": {
"gulp": "~3.5.6",
"gulp-uglify": "~0.2.1",
"gulp-jshint": "~1.5.0",
"gulp-jasmine": "~0.2.0",
"angular": "~1.2.10",
"karma": "~0.12.0",
"gulp-karma": "0.0.4",
"karma-jasmine": "~0.2.2",
"karma-chrome-launcher": "~0.1.2"
}
The message stating that module/angular is not defined means that your angular-mocks.js file is not being loaded, despite the fact you have it listed in your karma.conf.js file.
The problem you're experiencing is gulp-karma ignoring your karma.conf.js files array. This happens when you pass a string or glob into gulp.src in the gulpfile.
To work around this, pass gulp.src a string for a bogus file, "./foobar" for instance, and this will cause the files array in the karma.conf.js file to be used instead.
gulp.task('test', function () {
gulp.src('./foobar')
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}));
});
Hope this helps!
Reference: https://github.com/lazd/gulp-karma/issues/9
这篇关于与噶角测试:&QUOT;模块没有定义&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!