javascript - JS unit test with Jasmine -
i new jasmine, infact started today , don't have prior knowledge writing js unit test cases. trying write simple test case in jasmine in order test basic js code. have added necessary scripts , libraries in project.
so here piece of js code. fetching local australian date , time , arranging them in particular format.
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; } }
in above mentioned code can test various things example function fetching correct timezone, adding 0 prior time, formatting date, etc.
i know how structure test case. think of possible structure
describe( "australian full date format", function () { describe( "time format", function () { it("check if time format fetched correctly", function () { expect(something).toequal(something); }); }); describe( "adding 0 front", function () { it("check if 0 added prior time", function () { expect(something).toequal(something); }); }); describe( "get local australian time", function () { it("check if correct australian time fetched", function () { expect(something).toequal(something); }); }); it("check if date formatted correctly", function () { expect(something).toequal(something); }); });
if in right direction how move forward point onward. quite confused how write equivalent test case js code.
i create tests this:
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 front", function() { //... }); }); describe("if whatever >= 10", function() { it("should return original without changes", function() { //... }); }); }); //... });
so used describe
show function being tested. used describe
conditions. used it
test 1 feature of function. external objects (date
in case) should stubbed test functions getutctimeoffset
, getlocaltimefromaustraliatime
.
Comments
Post a Comment