我正在使用Gulp插件,允许使用以不同于英语的语言编写的保留字来编写JavaScript代码 - 它的主要功能是翻译,但它不是我的意思。
使用Mocha和Should.js编写单元测试我注意到我不能像这样使用for
循环:
describe('Proper translating JavaScript PL into JavaScript', function () {
var asserts = [
{
passed: '(tak || nie) && (prawda || fałsz)',
expected: '(true || false) && (true || false)'
},
{
passed: 'jeśli (tak) { jeżeli (prawda) {}}',
expected: 'if (true) { if (true) {}}'
},
{
passed: 'przez (zm i = 1; i <= 3; console.log(i++));',
expected: 'for (var i = 1; i <= 3; console.log(i++));'
},
{
passed: 'zm sum = funkcja (a, b) { zwróć a + b; };',
expected: 'var sum = function (a, b) { return a + b; };'
},
{
passed: 'generator snowball() { zm a = 1; dostarcz a++; }',
expected: 'function* snowball() { var a = 1; yield a++; }'
},
{
passed: 'lol',
expected: 'zap'
}
];
for (var i = 0, l = asserts.length; i < l; i++) {
var assert = asserts[i];
var desc = 'should turn `' + assert.passed + '` into `' + assert.expected + '`';
it(desc, function (done) {
tester(assert.passed, assert.expected, done);
});
}
});
当我执行上述操作时,一个错误被称为failing
与断言次数一样多。
如你所见,虽然有正确的2,但我们有6次失败。此外,这里只提到其中一种。
我不想双重描述如下的陈述:
describe('Proper translating JavaScript PL into JavaScript', function () {
var asserts = [
{
passed: '(tak || nie) && (prawda || fałsz)',
expected: '(true || false) && (true || false)'
},
{
passed: 'jeśli (tak) { jeżeli (prawda) {}}',
expected: 'if (true) { if (true) {}}'
},
{
passed: 'przez (zm i = 1; i <= 3; console.log(i++));',
expected: 'for (var i = 1; i <= 3; console.log(i++));'
},
{
passed: 'zm sum = funkcja (a, b) { zwróć a + b; };',
expected: 'var sum = function (a, b) { return a + b; };'
},
{
passed: 'generator snowball() { zm a = 1; dostarcz a++; }',
expected: 'function* snowball() { var a = 1; yield a++; }'
},
{
passed: 'lol',
expected: 'zap'
}
];
it('should be kicking 1', function (done) {
tester(asserts[0].passed, asserts[0].expected, done);
});
it('should be kicking 2', function (done) {
tester(asserts[1].passed, asserts[1].expected, done);
});
it('should be kicking 3', function (done) {
tester(asserts[2].passed, asserts[2].expected, done);
});
it('should be kicking 4', function (done) {
tester(asserts[3].passed, asserts[3].expected, done);
});
it('should be kicking 5', function (done) {
tester(asserts[4].passed, asserts[4].expected, done);
});
it('should be kicking 6', function (done) {
tester(asserts[5].passed, asserts[5].expected, done);
});
});
这些should be kicking N
并不重要 - 我一直懒得写下这样的东西:
it('should turn `(tak || nie) && (prawda || fałsz)` into `(true || false) && (true || false)`', function (done) {
tester('(tak || nie) && (prawda || fałsz)', '(true || false) && (true || false)', done);
});
无论如何,做独立的断言测试都没问题。
在这两种情况下前5次测试都成功,我们在这里根本不看它们。
我们关注的测试是放在asserts
中的这6个。
在分开的it()
事情中进行测试是正确的。
在for
循环中进行测试,一次失败会导致asserts.length
失败,另外一次失败会导致其他失败。
答案 0 :(得分:0)
将循环中的代码包装在闭包中:
for (var i = 0, l = asserts.length; i < l; i++) {
(function(n) {
var assert = asserts[n];
var desc = 'should turn `' + assert.passed + '` into `' + assert.expected + '`';
it(desc, function (done) {
tester(assert.passed, assert.expected, done);
});
})(i);
}
或使用asserts.forEach
。