Nick Nick - 2 years ago 204
Ruby Question

TimeWithZone & integration test fails

In a controller method I set a user's variable

equal to
when an activation email is sent to that user. On the development server this seems to work (although time expressions in my application are 2 hours behind on the local time of my computer).

I want to include an integration test that tests whether
indeed gets set properly. So I included the line:

assert_equal @user.activation_sent_at,

However, this produces the error:

No visible difference in the ActiveSupport::TimeWithZone#inspect output.
You should look at the implementation of #== on ActiveSupport::TimeWithZone or its members.

I think it's suggesting to use another expression for
in my test. I've looked at different sources, including, but am not sure what to do here. Any suggestions what could be causing this error?

Additional info: Adding
puts @stakeholder.activation_sent_at
confirms the two are equal. Not sure what generates the failure/error.

Answer Source

The issue is that the 2 dates are very close to each other but not the same. You can use assert_in_delta

assert_in_delta @user.activation_sent_at,, 1.second

For RSpec, a similar approach would be to use be_within:

expect(@user.activation_sent_at).to be_within(1.second).of

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download