`for` 循环中的 Mocha 单元测试和断言
Mocha unit tests and assertions in `for`loop
我正在开发 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
次断言的次数。
正如你所见,我们有 6 个失败,虽然有 2 个是正确的。而且,这里只提到其中一个。
我不想重复描述如下语句:
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
失败,另外一次失败会使其他失败。
将循环中的代码包装在闭包中:
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
.
我正在开发 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
次断言的次数。
正如你所见,我们有 6 个失败,虽然有 2 个是正确的。而且,这里只提到其中一个。
我不想重复描述如下语句:
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
.
分开进行测试 it()
一切顺利。
在 for
循环中进行测试,一次失败会使 asserts.length
失败,另外一次失败会使其他失败。
将循环中的代码包装在闭包中:
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
.