Stubbing Time.now in Ruby

Yeah, sure, we could do that. We could also use timecop. But both solutions seems overkill when all you need is to check if some attribute got updated with the result of `Time.now`. What you should do instead, is use the power of RSpec (or rspec-expectations to be more exact):

post.published_at.should be_within(1.second).of(Time.now)

Doesn’t it look just beautiful?

Published by

Paweł Gościcki

Ruby/Rails programmer.