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 date
s functionality, parts use...)
see this issue on angular. , this answer on stackoverflow.
Comments
Post a Comment