3 Reasons for Flaky RSpec Tests

Axel Kee on Ruby Yagi describes fixes for 3 common reasons for flaky RSpec tests.

1. Time dependant functions

  • Avoid inter-day flakes by anchoring with Date.today.beginning_of_day instead of variable 2.hours.ago
  • Specify the timezone with .in_time_zone instead of relying on the systems .to_time

2. Faker randomness

  • CCheck if the faker gem is giving unexpected output. Make sure to use URI encoding if a string is sent as URL parameters

3. External API calls

  • Use the VCR gem and rely on cached responses instead of hoping third parties are always up