我是摩卡新手,我想尝试使用 for 循环来创建测试用例。我想测试我制作的一个函数,该函数接受标准 12 小时时间的输入,并将其输出为 24 小时军用时间。这就是它的样子。
exports.main = function(time) {
var hr = parseInt(time.substr(0,2));
var period = time.substr(8,10);
if (period == 'AM' && hr == 12) {
hr = '0';
}
if (period == 'PM' && hr < 12) {
hr += 12;
}
hr.toString();
if (hr < 10) {
hr = '0' + hr;
}
return time = time.replace(/^\d{2}/g, hr).substr(0,8);
}
为了在摩卡中测试我的函数,我有两个数组,一个数组保存标准时间,另一个数组保存相应的预期输出。我想迭代它们并为每次迭代生成一个测试用例并测试我的函数。
test_array = ["12:00:00AM", "01:00:00AM", "02:00:00AM", "03:00:00AM", "04:00:00AM",
"05:00:00AM", "06:00:00AM", "07:00:00AM", "08:00:00AM", "09:00:00AM",
"10:00:00AM", "11:00:00AM", "12:00:00PM", "01:00:00PM", "02:00:00PM",
"03:00:00PM", "04:00:00PM", "05:00:00PM", "06:00:00PM", "07:00:00PM",
"08:00:00PM", "09:00:00PM", "10:00:00PM", "11:00:00PM"];
against = ["00:00:00", "01:00:00", "02:00:00", "03:00:00", "04:00:00",
"05:00:00", "06:00:00", "07:00:00", "08:00:00", "09:00:00", "10:00:00",
"11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00", "16:00:00",
"17:00:00", "18:00:00", "19:00:00", "20:00:00", "21:00:00", "22:00:00",
"23:00:00"]
这就是我的测试脚本的样子:
var converter = require('../modules/time.js');
describe('Time Converter', function() {
describe('main()', function() {
for(i = 0; i < 24; i++) {
it(test_array[i] + ' should convert to ' + against[i], function() {
var test = converter.main(test_array[i]);
assert.equal(test, against[i]);
});
}
});
});
以下是测试结果:
0 passing (23ms)
24 failing
1) Time Converter main() 12:00:00AM should convert to 00:00:00:
TypeError: Cannot read property 'substr' of undefined
at Object.exports.main (app/modules/time.js:43:27)
at Context.<anonymous> (app/test/test.js:35:26)
2) - 24) has the same result:
24) Time Converter main() 11:00:00PM should convert to 23:00:00:
TypeError: Cannot read property 'substr' of undefined
at Object.exports.main (app/modules/time.js:43:27)
at Context.<anonymous> (app/test/test.js:35:26)
但是当我将 for 循环更改为
for(i = 0; i < 23; i++)
所有测试都通过,但自然不会测试最后一个测试用例。
Time Converter
main()
✓ 12:00:00AM should convert to 00:00:00
✓ 01:00:00AM should convert to 01:00:00
✓ 02:00:00AM should convert to 02:00:00
✓ 03:00:00AM should convert to 03:00:00
✓ 04:00:00AM should convert to 04:00:00
✓ 05:00:00AM should convert to 05:00:00
✓ 06:00:00AM should convert to 06:00:00
✓ 07:00:00AM should convert to 07:00:00
✓ 08:00:00AM should convert to 08:00:00
✓ 09:00:00AM should convert to 09:00:00
✓ 10:00:00AM should convert to 10:00:00
✓ 11:00:00AM should convert to 11:00:00
✓ 12:00:00PM should convert to 12:00:00
✓ 01:00:00PM should convert to 13:00:00
✓ 02:00:00PM should convert to 14:00:00
✓ 03:00:00PM should convert to 15:00:00
✓ 04:00:00PM should convert to 16:00:00
✓ 05:00:00PM should convert to 17:00:00
✓ 06:00:00PM should convert to 18:00:00
✓ 07:00:00PM should convert to 19:00:00
✓ 08:00:00PM should convert to 20:00:00
✓ 09:00:00PM should convert to 21:00:00
✓ 10:00:00PM should convert to 22:00:00
23 passing (14ms)
但是,当我单独测试最后一个测试用例时,它通过了。
✓ 11:00:00PMshould convert to 23:00:00
因此,我很困惑为什么如果 for 循环迭代整个数组,所有测试都不起作用,但如果迭代直到最后一个索引,则所有测试都起作用。有人可以帮我解决这个问题吗?