Javascript Jasmine的JS单元测试
我对Jasmine很陌生,事实上我今天才开始写JS单元测试用例。我试图用Jasmine编写一个简单的测试用例,以测试一个非常基本的JS代码。我已经在我的项目中添加了所有必要的脚本和库 下面是一段JS代码。我正在获取澳大利亚当地的日期和时间,并以特定的格式进行安排Javascript Jasmine的JS单元测试,javascript,jquery,unit-testing,jasmine,Javascript,Jquery,Unit Testing,Jasmine,我对Jasmine很陌生,事实上我今天才开始写JS单元测试用例。我试图用Jasmine编写一个简单的测试用例,以测试一个非常基本的JS代码。我已经在我的项目中添加了所有必要的脚本和库 下面是一段JS代码。我正在获取澳大利亚当地的日期和时间,并以特定的格式进行安排 var date = { formatFullDate: function(date) { var localDate = this.getLocalTimeFromAustraliaTime(date),
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;
}
}
如果我的方向是正确的,那么我该如何从这一点开始前进。我很困惑如何为我的JS代码编写等效的测试用例。我将创建类似以下内容的测试:
describe("Australian full date format", function() {
describe("#formatFullDate", function() {
it("should have format 'date/month/year hour:min'", function() {
//...
});
});
describe("#formatTime", function() {
it("should have format 'hour:min'", function() {
//...
});
});
describe("#addZeroToFront", function() {
describe("if whatever < 10", function() {
it("should add 0 to front", function() {
//...
});
});
describe("if whatever >= 10", function() {
it("should return original without any changes", function() {
//...
});
});
});
//...
});
描述(“澳大利亚完整日期格式”,函数(){
描述(#formatFullDate),函数(){
它(“应具有格式‘日期/月/年小时:分钟’”,函数(){
//...
});
});
描述(#formatTime),函数(){
它(“应具有格式'hour:min',函数(){
//...
});
});
描述(#addZeroToFront),函数(){
描述(“无论什么<10”,函数(){
它(“应将0添加到前端”,函数(){
//...
});
});
描述(“无论什么>=10”,函数(){
它(“应返回原始文件而不作任何更改”,函数(){
//...
});
});
});
//...
});
因此,我使用
description
来显示正在测试的函数。然后使用描述
了解情况。使用it
仅测试函数的一个功能。所有外部对象(Date
在这种情况下)都应该被存根以测试函数getUTCtimeOffset
和getlocaltimeromaustratime
谢谢。你能给我一个例子吗?我在测试用例中实际在哪里使用日期对象来运行测试。嗯,你可以在测试中像普通一样使用日期对象,例如date.formatFullDate(testDate)
describe("Australian full date format", function() {
describe("#formatFullDate", function() {
it("should have format 'date/month/year hour:min'", function() {
//...
});
});
describe("#formatTime", function() {
it("should have format 'hour:min'", function() {
//...
});
});
describe("#addZeroToFront", function() {
describe("if whatever < 10", function() {
it("should add 0 to front", function() {
//...
});
});
describe("if whatever >= 10", function() {
it("should return original without any changes", function() {
//...
});
});
});
//...
});