Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 没有定义使用Karma-done()运行的Mocha单元测试_Javascript_Unit Testing_Mocha.js_Karma Runner_Karma Mocha - Fatal编程技术网

Javascript 没有定义使用Karma-done()运行的Mocha单元测试

Javascript 没有定义使用Karma-done()运行的Mocha单元测试,javascript,unit-testing,mocha.js,karma-runner,karma-mocha,Javascript,Unit Testing,Mocha.js,Karma Runner,Karma Mocha,我试图让用Mocha编写的测试运行Karma,它们也可以,但是我不能使用done()方法来实现异步测试,这基本上使这些工具对我来说毫无用处。我错过了什么 karma.conf.js module.exports = function(config) { config.set({ basePath: '../..', frameworks: ['mocha', 'requirejs', 'qunit'], client: { mocha: {

我试图让用Mocha编写的测试运行Karma,它们也可以,但是我不能使用done()方法来实现异步测试,这基本上使这些工具对我来说毫无用处。我错过了什么

karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '../..',
    frameworks: ['mocha', 'requirejs', 'qunit'],
    client: {
        mocha: {
            ui: 'bdd'
        }
    },
    files: [
      {pattern: 'libs/**/*.js', included: false},
      {pattern: 'src/**/*.js', included: false},
      {pattern: 'tests/mocha/mocha.js', included: false},
      {pattern: 'tests/should/should.js', included: false},
      {pattern: 'tests/**/*Spec.js', included: false},
      'tests/karma/test-main.js'
    ],
    exclude: [
      'src/main.js'
    ],
    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress', 'dots'],
    port: 9876,
    colors: true,
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_WARN,
    autoWatch: true,
    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['Chrome'],
    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,
    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};
test-main.js(配置RequireJS)

测试/fooSpec.js

define(['music/note'], function(Note) {

describe('nothing', function(done) {
    it('a silly test', function() {
        var note = new Note;
        note.should.not.eql(32);
    });
    done();
});
...
虽然这是一个精心设计的示例,但如果删除done()调用,它会成功。事实上,我得到了:

Uncaught TypeError: undefined is not a function
at /Library/WebServer/Documents/vg/tests/mocha/fooSpec.js:8
这是done()行。这是如何/为什么没有定义的?我不知道还有什么地方可以配置摩卡咖啡(或者用什么选项)。是否有某种全局名称空间或元编程魔法导致RequireJS干扰Mocha

我正在OSX10.9.2上的Chrome33上运行测试,以防有任何关联。我已经为此浪费了很多时间,准备放弃自动化测试:(-有类似的砖墙,有QUnit/Karma/RequireJS,但还没有找到任何替代方案来成功地自动化测试。我觉得自己像个白痴。

天哪%$\

一百万年来,我从未想过这会让我呕吐:

describe('nothing', function(done) {
    it('umm...', function() {
        var note = new Note;
        note.should.not.eql(32);
    });
    done(); // throws error that undefined is not a function
});
但这很管用:

describe('nothing', function(done) {
    it('umm...', function() {
        var note = new Note;
        note.should.not.eql(32);
    });
    setTimeout(function() {
        done();  // MAGIC == EVIL.
    }, 1000);
});

在摩卡咖啡中,
done
回调用于
it
之前
之后
之前
之后
。因此:

describe('nothing', function() {
    it('a silly test', function(done) {
        var note = new Note;
        note.should.not.eql(32);
        done();
    });
});

以下是。

您在该示例中运行的测试不需要done()回调。它不是异步的。一个何时需要done回调的示例

describe('Note', function() {
    it('can be retrieved from database', function(done) {
        var note = new Note();
        cb = function(){
           note.contents.should.eql("stuff retrieved from database");
           done()
        }
        //cb is passed into the async function to be called when it's finished
        note.retrieveFromDatabaseAsync(cb)
    });
});
您的测试不应该有一个已完成的回调

describe('nothing', function() {
    it('umm...', function() {
        var note = new Note;
        note.should.not.eql(32);
    });

});

只有“it”函数提供了一个已完成的回调。description不提供。您的问题不在于因果报应。您的摩卡测试定义不正确。

因此,我无法在几天内关闭我自己的问题。但也许我永远不会关闭。也许这个问题永远不应该被标记为已回答。谢谢您,否则我将永远无法返回;请添加一些为下一个可怜的草皮解释这些恶作剧的文档。我怀疑你说的“工作正常”的版本实际上产生了一个异常,最终被什么东西吞没了。当然,当它执行摩卡时,它不会在意。好吧,但它按照文档记录工作。测试按预期通过/失败。你是否建议如果客户机代码调用了undefined(),则初始化是不向它()传递任何内容,只异步(仅)捕获异常?我已经验证了它确实是这样做的。或者,也就是说,它静默地捕获异步抛出的错误(在测试用例返回后),这有一个净效果,您可以将done()用于descripe()案例,或者至少看起来是这样。我不确定这是否是可取的行为……你看了我写的答案了吗?我意识到了。我在编写任何真正的测试之前只尝试过API,所以一开始很惊讶done没有定义。如果我使我的测试异步,它似乎会被定义,我更惊讶。仅此而已很好,我已经整理好了,写了很多测试。
describe('nothing', function() {
    it('umm...', function() {
        var note = new Note;
        note.should.not.eql(32);
    });

});