I had a Rails spec that passed six days a week and failed only on Sundays. On a Sunday, RSpec reported this:
Failure/Error: expect(account.can_attend?(event)).to be false
expected false
got trueThe example stubs the membership to end tomorrow and expects the account to miss the event, which only works if the event starts more than a day from now:
# spec/models/account_spec.rb
it "is false when the membership ends before the event" do
allow(account.membership).to receive(:ends_at).and_return(1.day.from_now)
expect(account.can_attend?(event)).to be false
endWhat it means when a spec fails only on Sundays
The failure says can_attend? returned true, and it was right to. Here is the method:
# app/models/account.rb
# A member can attend if their membership lasts through the day of the event.
def can_attend?(event)
membership.ends_at.to_date >= event.starts_on
endOn a Sunday the membership ends on the day of the event, not before it, and the >= is satisfied. The spec's setup is what broke: on a Sunday it no longer says what the example's name says. It isn't flaky, it just gets its bad input once a week.
Two things keep it hidden. CI rarely runs on a Sunday, so an example like this can sit in the suite for weeks before it first runs on one (mine took a month). And "Sunday" means Sunday in Time.zone: if your app's time zone is UTC, that starts on Saturday afternoon or evening in US time zones, so the red build shows up after a Saturday evening push and looks like that push caused it.
Why 1.week.from_now.beginning_of_week is tomorrow on a Sunday
Here is the factory behind event:
# spec/factories/events.rb
FactoryBot.define do
factory :event do
starts_on { 1.week.from_now.beginning_of_week }
end
end1.week.from_now.beginning_of_week reads like "a week from now," but it is the Monday of the week that contains the day one week from now. Rails weeks start on Monday by default (Date.beginning_of_week is :monday), so how far away that Monday is depends on the weekday the code runs. On a Monday it is 7 days away. It loses a day for every day after that: 6 on a Tuesday, 2 on a Saturday, 1 on a Sunday.
On a Sunday that Monday is tomorrow, and 1.day.from_now is that same Monday. The spec assumed the event is more than a day away. The factory only makes that true six days out of seven.
The fix: derive the date from the record
The assertion is about the event, so compute the date from the event instead of from now:
it "is false when the membership ends before the event" do
allow(account.membership).to receive(:ends_at).and_return((event.starts_on - 1.day).end_of_day)
expect(account.can_attend?(event)).to be false
end
it "is true when the membership ends after the event" do
allow(account.membership).to receive(:ends_at).and_return((event.starts_on + 1.day).beginning_of_day)
expect(account.can_attend?(event)).to be true
endThe first membership ends at the end of the day before the event, the latest it can end and still miss it. The second ends at the start of the day after the event. Both dates come from event.starts_on, so neither depends on the weekday the suite runs.
When a spec needs a date before or after another record's date, derive it from that record.