Karma的angular度testing:“模块没有定义”

我知道这个问题已经被问了很多次了,我知道在大多数情况下,人们都缺lessangular-mocks.js文件。

我遇到了同样的问题,试图在模块上testing工厂。 不幸的是,我一直遇到与testing有关的问题(为什么,Angular,哦,为什么你必须假设一个windowdocument对象?),哪个状态,该模块没有定义。 我不知所措 我也试过使用angular.mocks.module,但是我得到一个消息,说Angular没有定义。 我究竟做错了什么?

值得注意的是,我正在使用gulp作为一个任务跑步者。 我的大文件(还不完美,任务没有链接):

 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']); 

我的karma.conf.js,由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 }); }; 

最后,我的testing套件(没有成立,如果我可以通过这个障碍,我们会很好):

 /* 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')); // }); }); 

有没有人看到任何错误? 我不确定是否有什么明显的东西我错过了 – 我可以使用额外的眼睛,或许多。 我原本以为我可以将它作为一个没有Karma的简单的Jasminetesting套件来运行,但是由于Angular,这有问题。 如果我不能得到这个工作,我可能只是使用npm的Angular包和改变我的Angular模块,以便它支持CommonJS …

感谢大家! 希望我不疯狂。

编辑:我已经包括我的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" } 

消息指出module / angular没有被定义,意味着你的angular-mocks.js文件没有被加载,尽pipe你已经在你的karma.conf.js文件中列出了它。

你遇到的问题是吞没你的karma.conf.js文件数组。 当你将一个string或者glob传递给gulp文件中的gulp.src时,会发生这种情况。

要解决这个问题,请将gulp.src作为伪造文件的string,例如“./ foobar”,这将导致使用karma.conf.js文件中的文件数组。

 gulp.task('test', function () { gulp.src('./foobar') .pipe(karma({ configFile: 'karma.conf.js', action: 'run' })); }); 

希望这可以帮助!

参考: https : //github.com/lazd/gulp-karma/issues/9