Angular的Karma模块测试是没有定义的

Angular testing with Karma: "module is not defined"

本文关键字:定义 模块测试 Karma Angular      更新时间:2023-09-26

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

我遇到了同样的问题,试图测试一个模块上的工厂。不幸的是,我一直遇到测试的问题(为什么,Angular,哦,为什么你必须假设一个windowdocument对象?),这表明模块没有定义。我很茫然。我也尝试过使用angular.mock。模块,但是我得到一个消息说Angular没有定义。我做错了什么?

值得注意的是,我使用gulp作为任务运行器。我的gulpfile(还不完美,任务没有链接):

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
  });
};

最后,我的测试套件(什么都没有设置,如果我能通过这个障碍,我们就好了):

/* 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的情况下将其作为一个简单的Jasmine测试套件运行,但由于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-mock .js文件没有被加载,尽管事实上你已经在karma.conf.js文件中列出了它。

你正在经历的问题是gulp-karma忽略了你的karma.conf.js文件数组。当您将字符串或glob传递给gulp时,就会发生这种情况。

要解决这个问题,通过gulp。SRC为一个伪造文件的字符串,"。例如,/foobar",这将导致使用karma.conf.js文件中的files数组。

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

希望这对你有帮助!

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

这不是你的问题的答案,但我在非常相似的情况下也有类似的症状。然而,根本原因是不同的,所以我将在这里分享它,以防其他人看到这篇文章,他们有和我一样的问题。

在我的例子中,因为我在游戏后期引入了测试,angular(1.4.7)和angular-mocks(1.6.9)的版本不匹配。

我发现这是根本原因通过在浏览器中调试Karma。降低angular-mock的版本以匹配angular解决了这个问题。