Mock date constructor with Jasmine

后端 未结 5 1830
轮回少年
轮回少年 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:33

    For the users who are using Edge version of Jasmine:

    it('Should spy on Date', function() {
        var oldDate = Date;
    
        // and.callFake
        spyOn(window, 'Date').and.callFake(function() {
            return new oldDate();
        });
    
        var d = new Date().toISOString;
    
        expect(window.Date).toHaveBeenCalled();
    });
    

提交回复
热议问题