最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angularjs - jasmine spyOn on javascript new Date - Stack Overflow

programmeradmin5浏览0评论

I am unit testing the my client code in angulajs and i understand this code means

 var newdate = new Date(2013,6,29);
    spyOn(Date.prototype, 'getTime').and.callFake(function() {
          return newdate;
        });

we mockout the getTime() methode of the Date object. but i want to mock out the new Date() instead. for example the code i want to test contains this line

payload.created_at = new Date();

I dont have access to payload.created_at. so i want to tell jasmine that whenever you see new Date(), replace that with a given date i give you. So i was thinking of something like but it doesnt work.

spyOn(Date.prototype, 'new Date').and.callFake(function() {
          return newdate;
        });

but new Date is not a method of the Date. Please can someone help me figure this out? thanks

I am unit testing the my client code in angulajs and i understand this code means

 var newdate = new Date(2013,6,29);
    spyOn(Date.prototype, 'getTime').and.callFake(function() {
          return newdate;
        });

we mockout the getTime() methode of the Date object. but i want to mock out the new Date() instead. for example the code i want to test contains this line

payload.created_at = new Date();

I dont have access to payload.created_at. so i want to tell jasmine that whenever you see new Date(), replace that with a given date i give you. So i was thinking of something like but it doesnt work.

spyOn(Date.prototype, 'new Date').and.callFake(function() {
          return newdate;
        });

but new Date is not a method of the Date. Please can someone help me figure this out? thanks

Share Improve this question asked Jun 2, 2015 at 17:15 user3137376user3137376 1,5272 gold badges19 silver badges29 bronze badges 5
  • Possible duplicate of stackoverflow.com/questions/26152796/… – PSL Commented Jun 2, 2015 at 17:20
  • I tried that, it didnt work. it gave me an undefined error – user3137376 Commented Jun 2, 2015 at 17:23
  • What undefined error? – PSL Commented Jun 2, 2015 at 17:23
  • @PSL , no worries, the link helped me figure out an answer. I posted my answer. Thanks – user3137376 Commented Jun 2, 2015 at 17:33
  • Ok. That is just difference in the the jasmine version (which you should have know already since in the question you are doing and.callFake). – PSL Commented Jun 2, 2015 at 17:38
Add a comment  | 

2 Answers 2

Reset to default 19

The Jasmine Clock api allows you to fake out the JavaScript Date functionality without manually writing a spy for it.

In particular, read the section about mocking the date.

describe("Mocking the Date object", function(){
    beforeEach(function() {
      jasmine.clock().install();
    });

    it("mocks the Date object and sets it to a given time", function() {
      var baseTime = new Date(2013, 9, 23);

      jasmine.clock().mockDate(baseTime);

      jasmine.clock().tick(50);
      expect(new Date().getTime()).toEqual(baseTime.getTime() + 50);
    });

    afterEach(function() {
      jasmine.clock().uninstall();
    });
});

so this link [Mock date constructor with Jasmine had the answer but it wasn't working for me for some reason. I guess it might i have to do with my jasmine version but below is the code that worked for me

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

there is a difference in the .and.callFake of the code above and that in the link above. Thanks

发布评论

评论列表(0)

  1. 暂无评论