Stubbing Time.current in Ruby specs

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):

expect(post.published_at).to be_within(1.second).of(Time.current)

Doesn’t it look just beautiful?

Published by

Paweł Gościcki

Ruby/Rails programmer.