【问题标题】:mocha as a library摩卡作为图书馆
【发布时间】:2012-03-30 08:55:24
【问题描述】:

我想使用 mocha(node.js 测试框架,而不是 ruby​​ 模拟库)作为库,而不是使用 mocha 可执行文件来运行我的测试。

是否可以通过这种方式运行 mocha 测试?这些示例都只是调用 mocha 库,假设它们已经“需要”,并且 mocha 可执行文件会提前完成所有“需要”,但我真的更喜欢在我的脚本中明确地执行它们,这样我就可以简单地在我的脚本上设置 +x 并直接调用它。

我可以这样做吗?

#!/usr/bin/env coffee
mocha = require 'mocha'
test = mocha.Test
suite = mocha.Suite
assert = require("chai").assert

thing = null

suite "Logging", () ->
  setup (done) ->
    thing = new Thing()
    done()
  test "the thing does a thing.", (done) ->
    thing.doThing () ->
      assert.equal thing.numThingsDone, 1
      done()
  teardown (done) ->
    thing = null
    done()

【问题讨论】:

    标签: javascript node.js coffeescript mocha.js


    【解决方案1】:

    有可能,但绝对不推荐。

    查看 mocha 二进制文件的源代码(特别是 bin/_mocha)以了解它的作用。特别是看run function。它使用的所有内容(RunnerReporter 等)都由 mocha 库导出,因此没有什么能阻止您对其进行逆向工程。

    【讨论】:

    • 这个已经过时了;如下所述,现在有官方支持,不需要逆向工程。
    【解决方案2】:

    以下 sn-p 允许您在 Node 之外以编程方式控制 Mocha 的主要功能,例如添加套件并以不同的步骤运行套件。关键点是找出如何让 mocha 接口在全球范围内可访问(代码也可以通过 gist 获得)

    var Mocha = require("mocha");
    var mocha = new Mocha();
    var _suites = [];
    var _done = false;
    
    /**
     * default mocha options
     * for other configurations, check out bin/_mocha
     */
    mocha.reporter("nyan");
    mocha.useColors(true);
    mocha.growl();
    
    module.exports = {
        /**
         * set interface (bdd is default) and make it global to node
         * @param  {string} interface bdd|tdd|exports|qunit
         */
        init: function(interface) {
            interface && mocha.ui(interface);
            mocha.suite.emit('pre-require', global, undefined, mocha);
        },
        /**
         * add suite
         * @param {function} suite to be executed later
         */
        add: function(suite) {
            mocha.suite && _suites.push(suite) && suite();
        },
        /**
         * run added suites
         */
        run: function(cb) {
            console.info('run mocha');
    
            var done = function () {
                _done = true;
                cb && cb();
                process.on('exit', function() {
                    console.info("exit mocha");
                    process.exit(1);
                });
            };
    
            mocha.run(done);
        },
        /**
         * end mocha test
         */
        exit: function() {
            if (_done) {
                process.exit();
            } else {
                var start = new Date().getTime(),
                interval = setInterval(function() {
                    if (_done) {
                        console.log("test suites finished");
                        clearInterval(interval);
                        process.exit();
                    } else if (new Date().getTime() - start > 5000) {
                        console.log("wait for nothing!");
                        clearInterval(interval);
                        process.exit();
                    }
                }, 250);
            }
        },
        /**
         * change mocha options at runtime, e.g., "reporter", "nyan"
         */
        _set: function(key, val){
            mocha[property](val);
        }
    };
    

    【讨论】:

      【解决方案3】:

      此功能已被添加。我在下面举了一个例子。

      我从here:得到信息

      您将需要 2 个文件。一项测试,一项运行测试。您可以将 runTest 标记为可执行文件,并在 mocha 选项中设置其输出。

      runTest.js

      #!/usr/bin/env node
      
      var Mocha = require('mocha'),
          fs    = require('fs'),
          path  = require('path');
      
      var mocha = new Mocha(
      {
        ui: 'tdd'     
      });
      
      mocha.addFile(
        path.join(__dirname, 'test.js')
      );
      
      mocha.run(function(failures){
        process.on('exit', function () {
          process.exit(failures);
        });
      });
      

      test.js

      var assert = require('chai').assert
      
      suite('Array', function(){
        setup(function(){});
        suite('#indexOf()', function(){
          test('should return -1 when not present', function(){
            assert.equal(-1, [1,2,3].indexOf(4));
          });
        });
      });
      

      【讨论】:

        猜你喜欢
        • 2012-09-18
        • 2019-01-22
        • 1970-01-01
        • 2016-02-05
        • 1970-01-01
        • 2022-07-20
        • 1970-01-01
        • 2015-05-27
        • 1970-01-01
        相关资源
        最近更新 更多