testunit - How to test simple ruby method, with timing and config dependencies -


i have method:

def self.should_restart?   if konfig.get(:auto_restart_time).present?     time.now>=time.parse(konfig.get(:auto_restart_time)) && utils.uptime_in_minutes>780   end end 

in regular ruby (not rails) how go testing this? monkeypatch konfig , utils return want seems soooo ugly.

you may able use timecop part of solution (which scores highly me "best-named gem, ever"). simple use, , patches sources of time data in sync, if example utils module uses standard methods assess time, should have same concept of "now" time.now shows.

note won't work if utils making call external api on process, in case should stub return uptime values needed in test assertions.

the following rspec snippet way of example, , making assumptions have available (such module under test being called server)

describe "#should_restart?"   before :each     timecop.travel( time.parse("2013-08-01t12:00:00") )     server.start # guess     # using `mocha` gem here     konfig.expect(:get).with(:auto_restart_time).returns( "18:00:00" )   end    after :each     timecop.return   end    "should false if server has been started"     server.should_restart?.should be_false   end    "should false before cutoff time"     timecop.travel( time.parse("2013-08-02t16:00:00") )     server.should_restart?.should be_false   end    "should true when server has been while, after cutoff time"     timecop.travel( time.parse("2013-08-02t18:05:00") )     server.should_restart?.should be_true   end end 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -