2016-10-18 29 views
1

我是摩卡新手,我想尝试使用for循环来创建测试用例。我想测试一个我需要输入标准12小时时间的函数,并将其输出到24小时军事时间。这就是它的样子。在摩卡测试中使用for循环

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循环遍历整个数组,但如果我重复,直到最后一个索引工作的所有测试不起作用。有人可以帮我解决这个问题吗?

回答

4

你的代码可能是同步的,但摩卡认为它是异步的。意思是:你的描述被解析为同步,摩卡存储这些信息并在它自己的上下文中运行每一个测试。这是异步的。为了使这项工作,你必须创建一个截流用功能:

var converter = require('../modules/time.js'); 
// outside the loop: 
function itShouldTestArray(i) { 
    // i is now within the function scope and won't change anymore 
    it(test_array[i] + ' should convert to ' + against[i], function() { 
    var test = converter.main(test_array[i]); 
    assert.equal(test, against[i]); 
} 
describe('Time Converter', function() { 
describe('main()', function() { 
    for(i = 0; i < 24; i++) { 
    itShouldTestArray(i); 
    }); 
    } 
}); 
}); 
0

我不知道摩卡好,所以我会按照约翰内斯对专家的意见。

虽然我确实有一些其他建议。

首先,这将是一个真正的维护头痛,有两个并行运行的阵列,如test_arrayagainst。很难看到这些,并确保正确的价值观匹配。

相反,把一切都变成一个数组,其中每个数组元素都包含两个测试值:

var tests = [ 
    '12:00:00AM = 00:00:00', 
    '01:00:00AM = 01:00:00', 
    '02:00:00AM = 02:00:00', 
    '03:00:00AM = 03:00:00', 
    '04:00:00AM = 04:00:00', 
    '05:00:00AM = 05:00:00', 
    '06:00:00AM = 06:00:00', 
    '07:00:00AM = 07:00:00', 
    '08:00:00AM = 08:00:00', 
    '09:00:00AM = 09:00:00', 
    '10:00:00AM = 10:00:00', 
    '11:00:00AM = 11:00:00', 
    '12:00:00PM = 12:00:00', 
    '01:00:00PM = 13:00:00', 
    '02:00:00PM = 14:00:00', 
    '03:00:00PM = 15:00:00', 
    '04:00:00PM = 16:00:00', 
    '05:00:00PM = 17:00:00', 
    '06:00:00PM = 18:00:00', 
    '07:00:00PM = 19:00:00', 
    '08:00:00PM = 20:00:00', 
    '09:00:00PM = 21:00:00', 
    '10:00:00PM = 22:00:00', 
    '11:00:00PM = 23:00:00', 
]; 

您可以使用split()在你的代码,这两个值中的每个数组元素分开。

接下来,从不硬编码for循环中的数组长度。相反,如果阵列的命名为test(如上例所示),请在循环中使用i < test.length。现在,如果您添加或删除条目,循环将仍然具有正确的长度。

但是,我根本不会使用for循环。您可以使用forEach数组方法获得更干净的代码。

以约翰内斯代码为起点,我将其更改为:

function itShouldTestTimes(test) { 
    var times = test.split(' = '); 
    var time12 = times[0], time24 = times[1]; 
    it(time12 + ' should convert to ' + time24, function() { 
     var result = converter.main(time12); 
     assert.equal(result, time24); 
    }); 
} 

describe('Time Converter', function() { 
    describe('main()', function() { 
     tests.forEach(itShouldTestTimes); 
    }); 
}); 
0

所以,我很困惑,为什么所有的测试不若连续工作循环迭代 通过整个数组,但如果我迭代直到最后一个索引 工程。有人可以帮我解决这个问题吗?

这可能是造成由摩卡进行的异步测试非常-难以调试问题。好像是从23到24某种程度上导致你的阵列被删除的进行的测试之前,改变i,赋予错误消息:

TypeError: Cannot read property 'substr' of undefined 

您可以避免通过指定它们的值测试时间在此之前删除的数组一个瓶盖内:

var converter = require('../modules/time.js'); 

describe('Time Converter', function() { 
    describe('main()', function() { 

     test_array = // insert array inside closure; 
     against = //insert array inside closure; 

     callback = function() { 

     var test = converter.main (test_array[i]); 
     assert.equal(test, against[i]); 
     } 

     for(i = 0; i < 24; i++) { 

      it(test_array[i] + ' should convert to ' + against[i], callback); 
     } 
     }) 
    }) 

这样每个callback传递给it()将有机会获得阵列而不被删除或测试时间之前改变他们的任何可能性。