javascript - How to create a spy on Date functions in jasmine testing -


i have following function , spec. have created spy on date objects passing , manipulating in function. spy not created on rightdate . can guys me solve ?

 var jasminefunc = new function(leftdate, rightdate){      var leftdateformat= leftdate.format("h:mm");      rightdate= new date(rightdate- 60000);      var rightdateformat = rightdate.format("h:mm");      }  spec:  describe("format", function(){     var leftdate = new date(2016, 0, 1, 0, 0, 0, 0);    var rightdate= new date(2016, 0, 1, 2, 0, 0, 0);     spyon(leftdate,"format").and,returnvalue("00:00");     spyon(rightdate, "format").and.returnvalue("00:59"); }); 

there few errors code.

  • there comma(,) between , & returnvalue, should have been dot (.)
  • there no function called format in date object, chrome didn't suggest method such
  • you have not used it function may analogous @test in java (see code below)
  • answering comment prototype, should work too, please replace gethours format on spy try out.

however i've mocked method for reference , works

var jasminefunc = function(leftdate, rightdate) {   var leftdateformat = leftdate.format("h:mm");   rightdate = new date(rightdate - 60000);   var rightdateformat = rightdate.format("h:mm"); }  date.prototype.format = function() {  return "this formats text"; }    describe("format", function() {  it('tempspec', function() {   var leftdate = new date(2016, 0, 1, 0, 0, 0, 0);   var rightdate = new date(2016, 0, 1, 2, 0, 0, 0);   spyon(leftdate, "gethours").and.returnvalue("00:00");   spyon(rightdate, "gethours").and.returnvalue("00:59");   spyon(leftdate, "format").and.returnvalue("i've hijacked in spy");    var dt = leftdate.gethours()   var txt = leftdate.format()   expect(dt).toequal("00:00");   expect(txt).toequal("i've hijacked in spy");   }) }); 
  • fyi here more information js date format, uses plain constructor format.
  • also out of curiosity, may ask why trying test api code, should testing own code because api's have been tested thoroughly

here updated version of code, think mean object getting changed, being reassigned. changed?

var jasminefunc = {   testfunc: function(leftdate, rightdate) {     var leftdateformat = this.formatfunc(leftdate);     rightdate = new date(rightdate - 60000);     var rightdateformat = this.formatfunc(rightdate);     return {'left' : leftdateformat, 'right' : rightdateformat}   },    formatfunc: function(value) {     return value.format("h:mm")   }  }  date.prototype.format = function() {   return "this formats text"; }   describe("format spec", function() {   it('tempspec', function() {     var leftdate = new date(2016, 0, 1, 0, 0, 0, 0);     var rightdate = new date(2016, 0, 1, 2, 0, 0, 0);     spyon(jasminefunc, "formatfunc").and.returnvalue("some arbitrary value");         var testobj = jasminefunc.testfunc(leftdate, rightdate);         expect(testobj.left).toequal("some arbitrary value");         expect(testobj.right).toequal("some arbitrary value");   });  }); 
  • since reusing variable, i've delegated format function can mock per requirement.
  • notice no longer have mock date prototype, can simple mock intermediate function , hence may reassign many times need.

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -