javascript - How to mock constructors in AngularJS(in particular Date() constructor) -


i need test function accepts millissinceepoch , returns time if date current date else returns time.

getlocaleabbreviateddatetimestring: function(millissinceepoch) {   var date = new date(millissinceepoch);   if (date.tolocaledatestring() == new date().tolocaledatestring()) {     // replace function removes 'seconds' time returned.     return date.tolocaletimestring().replace(/:\d\d /, ' ');   }   return date.tolocaledatestring(); 

i'm hoping test mocking date() constructor, i'm not sure how mock constructor using 'prototype'?

also, there better way test ?

i best not use date or math.random within code.

i create services replacing them, way can mock hearts content activating mock module.

angular.module('my.date',[])     .value('date', date);  angular.module('my.mock.date',[])     .value('date', function(){         // mock date code     }); 

(of course, don't need mock of dates functionality, parts use...)

see this issue on angular. , this answer on stackoverflow.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -