RSpec failures (exit 1, "expected ... got ...") in CI
RSpec ran the suite and one or more examples failed, so the process exits 1 and CI marks the job red. The failure block names the example and shows the expected versus actual value; the cause is usually an environment or ordering difference from local.
What this error means
The build ends with "N examples, M failures" and a non-zero exit. Each failure prints "expected: X / got: Y" with a backtrace, often only in CI.
Failures:
1) Invoice#total sums line items
Failure/Error: expect(invoice.total).to eq(100)
expected: 100
got: 0
# ./spec/models/invoice_spec.rb:12
3 examples, 1 failureCommon causes
Random order exposes inter-test coupling
RSpec randomizes order by seed; a test that relied on state from another fails when the order changes in CI.
Environment differences (time, locale, unprepared DB)
CI uses UTC, a fresh database, and freshly resolved gems, so time-, locale-, or data-sensitive expectations diverge from local.
How to fix it
Reproduce the CI seed and conditions
- Re-run with the seed RSpec printed to reproduce the order locally.
- Prepare the test database and match CI time/locale (TZ=UTC).
- Fix the coupled or environment-sensitive example.
bundle exec rspec --seed 12345Bisect an order-dependent failure
Let RSpec narrow the minimal set of examples that triggers the failure.
bundle exec rspec --bisect --seed 12345How to prevent it
- Keep examples independent so random order is safe.
- Prepare the database and set a fixed TZ/locale for tests.
- Pin gem versions so CI and local resolve the same.