Kevin Yang
2007-Jun-06 11:16 UTC
Is mock Object the best way to test scheduling system with a lot of Time.now?
Hi everyone, I hope everyone is doing well. I am working on a scheduling system that dealing with Time.now a lot. It is getting tricky to write test code for the function. suppose a function in a model: def expired? self.expire_time <= Time.now end i was wondering what is the best way to test it. Or should I say that I should design the function in the following way (more testable?) def expired?(time) self.expire_time <= time end then I should use it in the following way : object.expired? (Time.now) it is also easier to write test code. Any comments and suggestions will be greatly appreciated :) thanks Kevin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
James Mead
2007-Jun-06 11:27 UTC
Fwd: [Rails] Is mock Object the best way to test scheduling system with a lot of Time.now?
You could use Mocha (http://mocha.rubyforge.org) to temporarily stub the Time.now method for the duration of an individual test. Something like this... require ''mocha'' def test_me specific_time = Time.parse(''2007-01-01 06:30:00'') Time.stubs(:now).returns(specified_time) Time.now # => Mon Jan 01 06:30:00 +0000 2007 end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Kevin Yang
2007-Jun-06 11:34 UTC
Re: Fwd: [Rails] Is mock Object the best way to test scheduling system with a lot of Time.now?
Wow~~ This is genius ~~ how simply the dirty work could be done ! Great thanks to James :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---