我是编写单元测试的新手,需要一些帮助测试函数的一部分。使用moment.js进行单元测试
我的功能看起来像这样...
getData() {
return this.parameters.map(p => {
return {
name: p.name,
items: p.items.map(item => {
const toTime = item.hasOwnProperty('end') ? moment.utc(item.end._d).unix() : null;
const fromTime = item.hasOwnProperty('start') ? moment.utc(item.start._d).unix() : null;
return {
id: item.id,
fromTime: fromTime,
toTime: toTime,
};
}),
};
});
}
,到目前为止,我的测试看起来像这样(茉莉)
describe('getData()', function() {
it('should return json data', function() {
$ctrl.parameters = [{
name: 'test',
items: [{
id: 1,
fromTime: null,
toTime: null
}, {
id: 13,
fromTime: null,
toTime: null
}]
}];
expect($ctrl.getData()).toEqual([{
name: 'test',
items: [{
id: 1,
fromTime: null,
toTime: null
}, {
id: 13,
fromTime: null,
toTime: null
}]
}]);
});
});
此测试工作/传球,但你可以看到我我没有测试使用Moment.js的三元if/else。基本上,三元组的功能是检查项目是否包含名为start
/end
的属性,如果有,请将该值转换为epoch/unix时间戳,并将其分配给toTime
或fromTime
。所以如果物品有一个叫做end的属性,其值为'Sat Oct 31 2015 00:00:00 GMT+0000 (GMT)'
,那么它将被转换为'1446249600'
并分配给toTime
希望这可以解释它!我不确定如何为它编写测试,并希望得到任何帮助/建议。
谢谢!它的工作。也谢谢你的样品! –