Jos*_*ose 13 javascript unit-testing jasmine
我是新手编写单元测试,需要一些帮助来测试函数的一部分.
我的功能看起来像这样......
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,
};
}),
};
});
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我的测试看起来像这样(茉莉花)
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
}]
}]);
});
});
Run Code Online (Sandbox Code Playgroud)
这个测试正在工作/通过,但是你可以看到我没有测试使用Moment.js的三元if/else.基本上三元组所做的是检查项目是否包含名为start/ 的属性end,如果是,则将该值转换为epoch/unix时间戳并将其分配给toTime或者fromTime.因此,如果项目具有名为end的属性,其值为'Sat Oct 31 2015 00:00:00 GMT+0000 (GMT)',则将转换为'1446249600'并分配给toTime
希望这能解释它!我不确定如何为它编写测试,并希望得到任何帮助/建议.
最简单的选择是手动为输入构造几个示例日期.例如:
$ctrl.parameters = [{
name: 'test',
items: [{
id: 1,
start: moment.utc('2017-01-01T01:00:00'),
end: moment.utc('2017-01-01T06:00:00')
}, {
id: 13,
start: moment.utc('2017-01-02T08:00:00'),
end: null
}]
}];
Run Code Online (Sandbox Code Playgroud)
(注意在上面的例子中,我改变fromTime和toTime到start和end分别,因为这是getData被期望的输入.)
然后弄清楚他们的unix时间戳.您可以在外部执行此部分 - 例如,我刚刚在moment.js网站上打开了浏览器开发人员工具(F12),在控制台中评估了以下语句,并获取了时间戳值:
moment.utc('2017-01-01T01:00:00').unix()
moment.utc('2017-01-01T06:00:00').unix()
moment.utc('2017-01-02T08:00:00').unix()
Run Code Online (Sandbox Code Playgroud)
最后,回到单元测试中,只需验证时间戳是否与预期值匹配:
expect($ctrl.getData()).toEqual([{
name: 'test',
items: [{
id: 1,
fromTime: 1483232400,
toTime: 1483250400
}, {
id: 13,
fromTime: 1483344000,
toTime: null
}]
}]);
Run Code Online (Sandbox Code Playgroud)
或者,如果您不想在单元测试中使用硬编码时间戳,则可以将每个示例日期存储在其自己的变量中(例如start1,end1),然后比较,例如start1.unix():
// Arrange
const start1 = moment.utc('2017-01-01T01:00:00');
const end1 = moment.utc('2017-01-01T06:00:00');
const start2 = moment.utc('2017-01-02T08:00:00');
$ctrl.parameters = [{
name: 'test',
items: [{
id: 1,
start: start1,
end: end1
}, {
id: 13,
start: start2,
end: null
}]
}];
// Act
const result = $ctrl.getData();
// Assert
expect(result).toEqual([{
name: 'test',
items: [{
id: 1,
fromTime: start1.unix(),
toTime: end1.unix()
}, {
id: 13,
fromTime: start2.unix(),
toTime: null
}]
}]);
Run Code Online (Sandbox Code Playgroud)
这很好,因为单元测试是为了测试你的代码,而不是moment.js.由你决定.
另请注意,我使用Arrange-Act-Assert模式来组织测试.再一次,取决于你,但是一旦你的单元测试开始变得复杂,那往往会让事情变得更容易理解.
无论哪种方式,你将需要改变你如何计算toTime,并fromTime在你的getData方法,因为书面如果传递将无法工作的代码null进行任何start或end.特别是,item.hasOwnProperty('start')如果传入一个null值,它将返回true start,但它会因为尝试计算而出错item.start._d.相反,我建议将这两行更改为以下内容:
const toTime = item.end ? moment.utc(item.end._d).unix() : null;
const fromTime = item.start ? moment.utc(item.start._d).unix() : null;
Run Code Online (Sandbox Code Playgroud)
我还建议不要使用_dmoment对象的属性,因为那是一个内部(私有)变量.由于start并且end已经是时刻对象,您可以改为:
const toTime = item.end ? item.end.unix() : null;
const fromTime = item.start ? item.start.unix() : null;
Run Code Online (Sandbox Code Playgroud)
此处提供了包含上述所有建议更改的完整jsbin示例:
https://jsbin.com/xuruwuzive/edit?js,output
请注意,必须进行一些调整,使其在AngularJS的上下文之外运行(使getData成为一个独立的函数,直接接受其参数,而不是通过$ctrl).
| 归档时间: |
|
| 查看次数: |
3433 次 |
| 最近记录: |