循环摩卡testing?

我试图循环一个摩卡testing套件(我想testing我的系统与无数的值与预期的结果),但我不能得到它的工作。 例如:

spec / example_spec.coffee

test_values = ["one", "two", "three"] for value in test_values describe "TestSuite", -> it "does some test", -> console.log value true.should.be.ok 

问题是我的控制台日志输出如下所示:

 three three three 

我希望它看起来像这样:

 one two three 

我如何循环这些值为我的摩卡testing?

这里的问题是,你正在closures“值”variables,所以它总是会评估它的最后一个值。

像这样的东西可以工作:

 test_values = ["one", "two", "three"] for value in test_values do (value) -> describe "TestSuite", -> it "does some test", -> console.log value true.should.be.ok 

这是有效的,因为当值传递给这个匿名函数时,它被复制到外部函数中的新值参数中,因此不会被循环改变。

编辑:添加coffeescript“做”的好处。

您可以使用“数据驱动”。 https://github.com/fluentsoftware/data-driven

 var data_driven = require('data-driven'); describe('Array', function() { describe('#indexOf()', function(){ data_driven([{value: 0},{value: 5},{value: -2}], function() { it('should return -1 when the value is not present when searching for {value}', function(ctx){ assert.equal(-1, [1,2,3].indexOf(ctx.value)); }) }) }) })