尝试使用Mocha + NYC测试功能

时间:2018-11-03 12:03:47

标签: javascript node.js testing mocha nyc

我正在尝试使用mocha + nyc测试我的功能,以检查覆盖率。 这是要测试的功能:

function TypeCompare(Obj){

Obj.forEach(function (el){
  el.Age = StringToNumber(el.Age);
  el.Name = isValidString(el.Name);
  el.Surname = isValidString(el.Surname); 
  el.Mail = isEmail(el.Mail);
  el.DateofReg = StringToDate(el.DateofReg);
  el.Phone = isPhone(el.Phone);
  el.Time = StringToTime(el.Time);
  if(el.Age == "Age error" || el.Name == "Name/Sname error" || el.Surname == "Name/Sname error" || el.DateofReg == "Date error" || el.Time == "Time error" ||el.Mail == "Email error" || el.Phone == "Phone error"){
  invalid_types.push(el);
} else {
  valid_types.push(el);
}
});
RecordToDB(valid_types);
RecordToCSV(invalid_types);
}

获取对象数组的函数会与其他函数一起检查对象是否有效,并将有效对象推送到valid_types,将无效对象推送到invalid_types。 这是我的测试功能:

describe('Testing function TypeCompare which getting arr[objects] on input 
check arr for valid and invalid objects', function () {
  it('should return [valid_obj] and [] if all objects is valid', function () 
{
var arr = [{Age: '20', Name: 'Kiryl', Surname: 'Kazushchyk', Mail: 'kastys1@yandex.ru', DateofReg: '10-20-2015',
  Phone: '+375257794915', Time: '10:44'},
  {Age: 'fgh', Name: 'Kiryl', Surname: 'Kazushchyk', Mail: 'kastys1@yandex.ru', DateofReg: '10-20-2015',
    Phone: '+375257794915', Time: '10:44'}];
app.TypeCompare(arr);
// console.log(TypeCompare.valid_types);
// console.log(TypeCompare.invalid_types);
var expected_valid = [{Age: '20', Name: 'Kiryl', Surname: 'Kazushchyk', Mail: 'kastys1@yandex.ru', DateofReg: '2015-10-20',
  Phone: '+375257794915', Time: '10:44:00'}];
var expected_invalid = [{Age: 'Age error', Name: 'Kiryl', Surname: 'Kazushchyk', Mail: 'kastys1@yandex.ru', DateofReg: '2015-10-20',
  Phone: '+375257794915', Time: '10:44:00'}];

assert(app.invalid_types[0].Age === expected_invalid[0].Age);
assert(app.valid_types[0].Age === expected_valid[0].Age);
  });
});

我要导出此文件:

module.exports.TypeCompare =TypeCompare;
module.exports.valid_types = valid_types;
module.exports.invalid_types = invalid_types;

该测试通过了,但是我没有收到有关摘要的任何信息: test result without coverage

如果我测试其他功能,则一切正常:test result other functions

有人可以告诉我是什么问题吗?

1 个答案:

答案 0 :(得分:0)

已解决。问题出在用TypeCompare调用的函数中