I have the following function
testSomething: function() {
var dateTimeNow = new Date();
return dateTimeNow;
}
and i'd like to use the spyOn function to stub out the date and return
a particular date time so that I can test date specific conditions. I
can't work out how to do it.
cheers
Alex
Alex
--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.
testDate = new Date('2011-01-01 03:00');
expect(session.endOfDay()).toEqual(new Date('2011-01-02 00:00'));
testDate = new Date('2011-01-01 05:00');
expect(session.endOfDay()).toEqual(new Date('2011-01-02 00:00'));
testDate = new Date('2011-01-01 23:59')
expect(session.endOfDay()).toEqual(new Date('2011-01-02 00:00'));
The issue i'm getting now is that you can only use the spyOn function
once (i can't replace it 4 times in a row, and that when i pass
testDate into andReturn you can no longer change the variable (i.e
it's then bound).
Is my only option to split this 1 test into 4 different tests?
spyOn(obj, "method").andReturn(val1);
// test stuff
obj.method.andReturn(val2);
// test more stuff
obj.method.andReturn(val3);
--
Ben Loveridge
var testDate = 'creating closure to use this later';
var dateFunc = function() {
return testDate;
}
spyOn(MyApp.util, 'now').andCallFake(dateFunc)
The library Timecop.js might be of interest.
https://github.com/jamesarosen/Timecop.js
It provides controllable Date behaviour.
--
Rob Hunter
(typed with my thumbs)