Coffeescript 摩卡安魂曲

Coffeescript 摩卡安魂曲,coffeescript,requirejs,mocha.js,Coffeescript,Requirejs,Mocha.js,RequireJS和摩卡在一起工作时遇到了一些问题。 我认为这是因为mocha不会等待requireJS的异步操作完成,而是决定测试已经完成 作为一个热修复程序,我将requireJS的加载调用包装在mocha的it()调用中。 莫名其妙地,mocha知道当我添加回调时,它应该等待异步方法完成 但是我想知道是否没有比我现在使用的更方便的设置了。当前的设置既不好也不灵活 这是我的测试。咖啡脚本: describe 'Ink', -> describe '#constructor',

RequireJS和摩卡在一起工作时遇到了一些问题。 我认为这是因为mocha不会等待requireJS的异步操作完成,而是决定测试已经完成

作为一个热修复程序,我将requireJS的加载调用包装在mocha的
it()
调用中。 莫名其妙地,mocha知道当我添加回调时,它应该等待异步方法完成

但是我想知道是否没有比我现在使用的更方便的设置了。当前的设置既不好也不灵活

这是我的测试。咖啡脚本:

describe 'Ink', ->
    describe '#constructor', ->
        it 'should return an Ink instance', ( done ) ->
            requirejs [ "build/ink/core/Ink" ], ->
                # commence testing
                a = new Ink( '<div></div>' )
                assert.equal( new Ink instanceof Ink, false )
                assert.equal( new Ink instanceof window.jQuery, true )

                done()

describe 'Mixin', ->

    f : ( Mixin ) ->
        # test mixin
        class A

            constructor : ( @a ) ->

        class m extends Mixin

            constructor : () -> @mixin_prop = 42
            increment : ( arg ) -> return arg + 1

        class B extends A
            Mixin.mixin( m, @ )

        b = new B()

        return b

    it 'should chain the constructor', ( done ) ->
        requirejs [ "build/ink/core/Mixin" ], ( Mixin ) ->
            b = f( Mixin )
            assert.equal( b.mixin_prop, 42 )
            done()

    it 'should add the methods from the mixin to the new class', ( done ) ->
        requirejs [ "build/ink/core/Mixin" ], ( Mixin ) ->
            b = f( Mixin )
            assert.equal( b.increment( 42 ), 42 )
            done()
描述‘墨水’,->
描述“#构造函数”,->
它“应该返回一个墨水实例”(完成)->
requirejs[“构建/墨水/核心/墨水”],->
#开始测试
a=新墨水(“”)
assert.equal(墨水的新墨水实例,false)
assert.equal(window.jQuery的新墨水实例,true)
完成()
描述“混合”,->
f:(混合)->
#试验混合素
甲级
构造函数:(@a)->
类m扩展了Mixin
构造函数:()->@mixin_prop=42
增量:(arg)->返回arg+1
B类扩展了A类
Mixin.Mixin(m,@)
b=新的b()
返回b
它“应该链接构造函数”(完成)->
requirejs[“build/ink/core/Mixin”],(Mixin)->
b=f(混合)
断言相等(b.mixin_prop,42)
完成()
它“应该将mixin中的方法添加到新类中”,(完成)->
requirejs[“build/ink/core/Mixin”],(Mixin)->
b=f(混合)
断言相等(b.增量(42),42)
完成()

我在测试中没有使用requirejs,但我认为
之前的
函数可能会有所帮助:

describe 'Mixin - ', ->
  before (done) ->
    console.log dateFormat(new Date(), "HH:MM:ss");
     requirejs [ "build/ink/core/Ink" ], ->
                # commence testing
                a = new Ink( '<div></div>' )
                assert.equal( new Ink instanceof Ink, false )
                assert.equal( new Ink instanceof window.jQuery, true )
    done()
  beforeEach ->
    ....
  describe ... 
描述“Mixin-”,->
之前(完成)->
console.log dateFormat(新日期(),“HH:MM:ss”);
requirejs[“构建/墨水/核心/墨水”],->
#开始测试
a=新墨水(“”)
assert.equal(墨水的新墨水实例,false)
assert.equal(window.jQuery的新墨水实例,true)
完成()
每次之前->
....
描述。。。

我在beforeach中初始化模块,并使用回调触发异步:

describe...
    var Module
    beforeEach(function(callback){
        requirejs
            Module = loadedFile
            callback(); // suites will now run
    })

我这里有一个引导程序:

Mocha提供了一个
done
回调,用于调用
it
的函数,它可以很好地实现这一目的。下面是一个我目前如何使用它的示例——请注意,我也在使用require加载我的测试配置,显然这是纯JS,不是CoffeeScript,但它应该获得

define([
  'chai',
  'SystemUnderTest'
], function(chai, SystemUnderTest) {

var expect = chai.expect;

describe('A functioning system', function() {

  it('knows when to foo', function(done) {
    sut = new SystemUnderTest();
    expect(sut.foo()).to.be.ok;
    done();
  });
});
因此,mocha对异步测试的支持(通常用于测试异步服务)也可用于支持异步加载模块的测试