3

RequireJS と mocha の連携には問題があります。これは、モカがrequireJSの非同期操作が完了するのを待たず、テストが完了したと判断したためだと考えました。

ホット フィックスとして、 requireJS の読み込み呼び出しを mocha のit()呼び出しにラップしました。非同期メソッドが完了するまで待機する必要があることを、コールバックを追加するときにどういうわけかモカは知っています。

しかし、私が今使っているものよりも便利なセットアップが他にないかどうか知りたい. 現在のセットアップは、あまり良くも柔軟でもありません。

これは私の test.coffee スクリプトです:

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()
4

3 に答える 3

3

モジュールを beforeEach で初期化し、コールバックを使用して非同期をトリガーします。

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

ここにブートストラップがあります: https://github.com/clubajax/mocha-bootstrap

于 2013-03-17T18:19:14.230 に答える
1

Mocha は、doneで呼び出す関数へのコールバックを提供しit、この目的にはうまく機能します。これは私が現在それをどのように使用しているかの例です。テスト構成をロードするためにrequireも使用していることに注意してください。明らかにこれはCoffeeScriptではなくストレートなJSですが、取得する必要があります。

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 のサポートは、非同期にロードされたモジュールのテストをサポートするためにも使用できます。

Mocha の非同期ドキュメント

于 2013-06-20T21:40:55.107 に答える
0

beforeテストで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 ... 
于 2013-02-08T20:50:03.150 に答える