Travis builds fail unnecessarily because of too tight timing constraints in specs.
Created by: jhund
The spec below fails on Travis because it expects two events to happen at the same second. This expectation is valid in a production system, however not on Travis where things can be a lot slower.
Notice that the expected and received value differ by 1 second. This is probably caused by delays in ActiveRecord callbacks running on a slow CI server.
Failures:
1) Project last_activity methods last_activity_date returns the creation date of the project's last event if present
Failure/Error: project.last_activity_at.to_i.should == last_event.created_at.to_i
expected: 1403898527
got: 1403898526 (using ==)
# ./spec/models/project_spec.rb:118:in `block (4 levels) in <top (required)>'
To fix it, I propose to make the time comparison less stringent and allow a difference of up to 1 second.
Also I believe that this particular spec has a logic error. It's titled to test project.last_activity_date
, however in the test it references project.last_activity_at
instead.
The code under test:
def last_activity_date
last_activity_at || updated_at
end
I'd be happy to submit a pull request that changes
it 'returns the creation date of the project\'s last event if present' do
last_activity_event = create(:event, project: project)
project.last_activity_at.to_i.should == last_event.created_at.to_i
end
to
it 'returns the creation date of the project\'s last event if present' do
last_activity_event = create(:event, project: project)
(project.last_activity_date - last_event.created_at).to_i.abs.should < 2
end
I checked and didn't find any other instances in model specs were times were cast to integer for comparison.
This bug makes my pull request fail, so I'm highly motivated to fix this. I'm happy to prepare a pull request if this is something you'd like to address.