【发布时间】:2014-03-21 10:59:10
【问题描述】:
我对 Jasmine 很陌生,事实上我今天才开始并且没有编写 JS 单元测试用例的先验知识。我正在尝试在 Jasmine 中编写一个简单的测试用例,以测试一个非常基本的 JS 代码。我在我的项目中添加了所有必要的脚本和库。
所以这是一段 JS 代码。我正在获取澳大利亚当地的日期和时间,并以特定格式排列它们。
var date = {
formatFullDate: function(date) {
var localDate = this.getLocalTimeFromAustraliaTime(date),
month = this.addZeroToFront(localDate.getMonth() + 1),
hour = this.addZeroToFront(localDate.getHours()),
minute = this.addZeroToFront(localDate.getMinutes());
return localDate.getDate() + '/' + month + '/' + localDate.getFullYear() + ' ' + hour + ':' + minute;
},
formatTime: function(date) {
var localDate = this.getLocalTimeFromAustraliaTime(date),
hour = this.addZeroToFront(localDate.getHours()),
minute = this.addZeroToFront(localDate.getMinutes());
return hour + ':' + minute;
},
addZeroToFront: function(whatever) {
if (whatever < 10) whatever = "0" + whatever;
return whatever;
},
getUTCtimeOffset: function() {
var date = new Date();
return date.getTimezoneOffset();
},
getLocalTimeFromAustraliaTime: function (date) {
var utcTime = new Date(date.getTime() - 11*60*60*1000),
localDate = new Date(utcTime - this.getUTCtimeOffset()*60*1000);
return localDate;
}
}
在上面提到的代码中,我可以测试各种事情,例如函数正在获取正确的时区、在时间之前添加 0、格式化日期等。
我想知道如何构建我的测试用例。我能想到可能的结构
describe( "Australian full date format", function () {
describe( "Time format", function () {
it("Check if time format is fetched correctly", function () {
expect(something).toEqual(something);
});
});
describe( "Adding 0 to the front", function () {
it("Check if 0 is added prior to the time", function () {
expect(something).toEqual(something);
});
});
describe( "Get local Australian time", function () {
it("Check if correct Australian time is fetched", function () {
expect(something).toEqual(something);
});
});
it("Check if the date is formatted correctly", function () {
expect(something).toEqual(something);
});
});
如果我的方向是正确的,那么我该如何从这一点开始前进。我很困惑如何为我的 JS 代码编写等效的测试用例。
【问题讨论】:
标签: javascript jquery unit-testing jasmine