Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
Using jasmie .toHaveBeenCalledWith() function with Moment Objects

I have the following test in a angular (4) -

 it('should call service',
    async(inject([CommentTableComponent], (cmp: CommentTableComponent) => {
      spyOn(mockSock, 'getComments').and.callThrough();
      cmp.ngOnInit();
      expect(mockSock.getComments).toHaveBeenCalledWith('api/jobs/604', moment());
    })));

which throws the error:

Expected spy getComments to have been called with [ 'api/jobs/604', Sat Apr 29 2017 12:31:07 GMT+0100 ] but actual calls were [ 'api/jobs/604', Sat Apr 29 2017 00:00:00 GMT+0100, Sat Apr 29 2017 12:31:07 GMT+0100 ].

As these strings are clearly identical, I assume this is because the test value of now is several milliseconds later than the value when the component was running and the milliseconds aren't parsed into the string.

So my question is, how can I make my test work, is there a way to check the parameter called with is within x milliseconds of the current time?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try this:

var spy = spyOn(mockSock, 'getComments').and.callThrough();
var today = moment('2017-4-29').toDate();
jasmine.clock().mockDate(today);
expect(spy.calls.mostRecent().args[0]).toEqual('api/jobs/604');
expect(spy.calls.mostRecent().args[1].valueOf()).toEqual(today.valueOf());

Reference: https://jasmine.github.io/2.5/introduction.html#section-Jasmine_Clock

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!