获取给定测试的{moc`sign`调用列表

时间:2016-05-28 04:03:52

标签: javascript unit-testing mocha

有没有办法让一组消息传递给describe

我想从下面testList调用中作为消息传递的值动态创建describe数组。

示例测试



<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.min.css" rel="stylesheet"/>
<div id="mocha"></div>
<script>
    mocha.setup('bdd');
</script>
<script>
    mocha.checkLeaks();
    //mocha.globals(['jQuery']);
</script>
<script>
    var expect = chai.expect;
    var testList = ['methodTrue', 'methodFalse', 'methodIdentity'];
    var testObject = {
        methodTrue: function () {return true;},
        methodFalse: function () {return false;},
        methodIdentity: function (n) {return n;}
    }
    describe('testObject', function () {
        describe('methodTrue', function () {
            it('should be a method', function () {
                expect(testObject.methodTrue).to.be.a('function');
            });
        });
        describe('methodFalse', function () {
            it('should be a method', function () {
                expect(testObject.methodFalse).to.be.a('function');
            });
        });
        describe('methodIdentity', function () {
            it('should be a method', function () {
                expect(testObject.methodIdentity).to.be.a('function');
            });
        });
        it('should have a method for every test', function () {
            Object.keys(testObject).forEach(function (v, i) {
                expect(testList.indexOf(v), 'no test for ' + v).to.be.above(-1);  
            });
        });
    });
    mocha.run();
</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

你可能会看看Mocha的来源并想出如何走一个测试套件。但是,这是一种方法,它不依赖于了解内部结构,并且如果内部结构发生变化则不会中断。策略是用您自己的函数替换describe,该函数记录传递给它的内容,以便您以后使用它。我在命令行使用了Mocha,但是在一个意味着在Node中运行的套件和一个意味着在浏览器中运行的套件之间没有区别。

var blocks = [];

function wrapperDescribe() {
    // It is generally unsafe to just leak `arguments` objects. So we
    // slice it to make a copy before pushing it into `blocks`.
    var args = Array.prototype.slice.call(arguments);
    blocks.push(args);
    describe.apply(this, args);
}

(function () {
    // We do not do this at the top level because it would modify a
    // global used by all Mocha files. Whether or not you do want this
    // depends on the needs to you project.
    var describe = wrapperDescribe;

    function fn () {}

    describe("one", function () {
        it("test 1", fn);
        it("test 2", fn);
    });

    describe("two", function () {
        it("test 1", fn);
        it("test 2", fn);
    });
}());

console.log(blocks);

输出:

$ ./node_modules/.bin/mocha 
[ [ 'one', [Function] ], [ 'two', [Function] ] ]


  one
    ✓ test 1
    ✓ test 2

  two
    ✓ test 1
    ✓ test 2


  4 passing (6ms)

在运行测试之前输出数组,这对于Mocha来说是正常的。 (Mocha首先读取所有测试,执行所有describe回调,然后运行测试。)

要使此功能仅在describe块的子集上有效,您可以不覆盖describe,而是直接根据需要调用wrapperDescribe