I have a problem where I’m validating that the current time has been inserted into the html (used to do a sort of time_ago with javascript).
The current unix time (epoch) is rendered in the given html element’s data data attribute, in this case like this:
<a href="/somewhere" data-last-points-awarded-at="1331606458">Something Happend</a>
But when my test runs:
recipe = Factory(:recipe, name: "Apple Pie", user: @user)
visit some_path
time_in_layout = find('a[data-last-points-awarded-at]')['data-last-points-awarded-at']
current_time = Proc.new { Time.now }.call.to_i.to_s
time_in_layout.should eql(current_time)
Upon running my test it fails… but just barely:
Failure/Error: time_in_layout.should eql(current_time)
expected: "1331606459"
got: "1331606458"
(compared using eql?)
Everything is fine, the test is pretty much the way I actually want it to work however, the proc returns a time which is a second later, how can I fix this? Like how can I lock the time at the beginning of the test, can I even do such a thing?
Check out TimeCop, as it does exactly what you want.