In an rspec-rails suite, what does config.use_transactional_fixtures = true do to each example, and which cases still leave rows behind or need it off?
answer
- begin before, roll back after
- use_transactional_tests underneath
- one pinned connection shared across threads
- before(:context) runs outside the transaction
- uses_transaction opts one example out
basics
~20 suse_transactional_fixtures = true wraps every example in a database transaction that is rolled back afterwards, sharing one pinned connection across threads. Rows written in before(:context), by another process or by code needing separate connections escape it; those cases need it off.
solid answer
~40 sThe setting feeds Active Record's `use_transactional_tests`. Before each example rspec-rails pins one connection per writing pool and begins a non-joinable transaction; after the example it rolls back, so rows from the example and its `before` hooks vanish without truncation. Because the connection is pinned with thread locking, the Puma thread in a system spec uses the same connection and sees uncommitted rows, so the old truncation setups for browser specs are unnecessary. Model transactions inside become savepoints, and their `after_commit` callbacks still run. What escapes: data created in `before(:context)`, which runs outside the per-example transaction; rows written by a separate process, such as a `bin/jobs` worker; and tests that need two real connections, like lock contention. Turn it off per group with `self.use_transactional_tests = false` or per example with `uses_transaction`, and clean up yourself.
code
ruby · 13 linesRSpec.describe "Timesheet locking", type: :model do
# two real connections must contend, so this group commits for real
self.use_transactional_tests = false
after { Timesheet.delete_all }
it "blocks a second approver while the row is locked" do
sheet = Timesheet.create!(week: 40)
sheet.with_lock do
# a second connection, opened in another thread, would block here
end
end
endgo deeper
Know that each example's database changes are rolled back, so tests do not see each other's records.
Explain the pinned connection and per-example transaction, and why before(:context) data is not rolled back.
Show when a test needs real commits or separate connections, how to opt out per group or per example, and how to clean up without breaking parallel runs.
Set the suite policy: transactional by default, a small, isolated set of committing tests, and how that interacts with parallel workers and multiple databases.
## What the setting turns on `config.use_transactional_fixtures = true` is written into every `rails_helper.rb` that `rspec:install` generates for an Active Record app. Despite the name, it is not about fixture files: it decides whether each example runs inside a **database transaction that is rolled back at the end**. rspec-rails copies the value into Active Record's `use_transactional_tests` class attribute on every example group (the setting also has the alias `use_transactional_examples`). What happens around one example: 1. For every writing connection pool, Active Record **pins** a connection with `pin_connection!` and begins a transaction with `joinable: false`. 2. The example and its `before` and `after` hooks run. Every insert, update and delete goes through that pinned connection. 3. After the example, `unpin_connection!` rolls the transaction back. If something committed or rolled it back early, Rails resets the connection and stops trusting its cached fixtures. Pools connected later in the example, for example a second database reached for the first time, are subscribed to and wrapped the same way. ## Why system specs work with it The pinned connection is shared by **every thread**, and `lock_threads` (default `true`) serialises access to it. A system spec starts the app in a Puma server thread; that thread reads through the same connection, so it sees the timesheet the example created before calling `visit`. Older suites often switched transactions off for browser specs and truncated tables instead; with a shared, pinned connection that workaround is unnecessary for system specs on Rails 8.1. Because the outer transaction is non-joinable, a `transaction` block in model code opens a **savepoint**, and when it completes Active Record runs its commit callbacks. `after_commit` hooks therefore fire in transactional specs, even though nothing is ever committed to the database. ## What still leaves rows behind | Case | Why it escapes | What to do | |---|---|---| | `before(:context)` / `before(:all)` setup | runs once, outside any example's transaction | create per example, or delete in `after(:context)` | | a separate OS process (a `bin/jobs` worker, a forked script) | its own connection never sees or joins the transaction | test the job in-process, or disable transactions and clean up | | a raw database client opened outside Active Record | it holds its own session, not the pinned connection | read through Active Record, or disable transactions | rspec-rails also prints a warning when a fixture accessor such as `timesheets(:week_40)` is called in a `before(:context)` hook. ## When to switch it off Some tests need real commits or several independent connections: - **Concurrency tests** that prove row locking, `lock!` or a unique index under a race need two connections that block each other; a single shared connection cannot show contention. - **Multi-process tests** where a separate worker process must read what the example wrote. - **Code that inspects the transaction state**, such as asserting that something happens only outside any open transaction. The switches, narrowest first: - `uses_transaction "locks the timesheet row"` inside a group opts out the example with that description; rspec-rails compares it with the example's description. - `self.use_transactional_tests = false` inside a `describe` turns it off for that group. - `config.use_transactional_fixtures = false` turns it off for the suite, which you should only do with a cleanup strategy that truncates or deletes after each example. Any opt-out leaves committed rows, so the group must remove them itself, and parallel workers must not share those tables. ## Several databases Transactions are opened per **writing connection pool**. An app that also writes to a second database, such as a reporting database configured in `database.yml`, gets a pinned connection and a rolled-back transaction on each pool it touches. When one database must commit for real, `skip_transactional_tests_for_database(:reporting)` in the example group turns transactions off for that database alone and leaves the primary one transactional. ## Common misunderstandings - The setting does not load fixture files; `fixtures :all` or `config.global_fixtures` does that. - Turning it off does not make specs "more realistic" for ordinary model and request specs; it makes them slower and order-dependent. - A test that passes only with transactions off often hides a dependency on committed data that the code under test should not have.
- Why does an after_commit callback fire in a transactional spec when nothing is committed?The test transaction is begun with `joinable: false`. When model code calls `transaction` or `save`, Active Record opens a savepoint inside it, and because the enclosing transaction is not joinable it runs the commit callbacks when that savepoint completes. The data is still rolled back when the example ends.
- Why can a system spec's browser see records the example created before visit?Active Record pins one connection per pool for the whole example and shares it across threads, serialising access with `lock_threads`. The Puma thread serving the browser uses that same connection, so it reads the uncommitted rows inside the example's transaction.
A transactional example is a meeting room whiteboard that is wiped after every meeting: whatever a meeting writes disappears, but a notice pinned to the corridor wall before the meetings started, like before(:context) data, stays up for everyone.
- use_transactional_fixtures only matters when YAML fixture files exist
- System specs need transactions off and table truncation on Rails 8.1
- Rows created in before(:context) are rolled back after each example
- after_commit callbacks never run while transactional examples are on
- A separate bin/jobs process sees the example's uncommitted rows