Mock date constructor with Jasmine

后端 未结 5 1808
轮回少年
轮回少年 2021-01-07 16:46

I\'m testing a function that takes a date as an optional argument. I want to assert that a new Date object is created if the function is called without the argument.

5条回答
  •  花落未央
    2021-01-07 17:26

    this worked for me

    var baseTime = new Date().getTime();
    spyOn(window, 'Date').and.callFake(function() {
       return {getTime: function(){ return baseTime;}};
    });
    

提交回复
热议问题