【问题标题】:Requirejs with mochaRequirejs 和 mocha
【发布时间】:2013-06-21 05:40:55
【问题描述】:

RequireJS 和 mocha 在协同工作时遇到了一些问题。 我认为这是因为 mocha 不会等待 requireJS 的异步操作完成并决定测试完成。

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

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

这是我的 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()

【问题讨论】:

    标签: coffeescript requirejs mocha.js


    【解决方案1】:

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

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

    我在这里有一个引导程序:https://github.com/clubajax/mocha-bootstrap

    【讨论】:

      【解决方案2】:

      Mocha 为您使用it 调用的函数提供了一个done 回调,它可以很好地用于此目的。这是我目前如何使用它的一个示例——注意我也在使用 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 对异步测试的支持(您可能“通常”用于测试异步服务)也可用于支持异步加载模块的测试。

      Mocha's async documentation

      【讨论】:

        【解决方案3】:

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

        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 ... 
        

        【讨论】:

          猜你喜欢
          • 2017-08-28
          • 2014-05-14
          • 2013-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多