Ruby on Rails Quality Control & Assurance 2 — Questions and Answers
Question 1: Which RSpec matcher should you use to verify that a method raises a specific exception class?
- expect { }.to raise_error(ErrorClass) (Correct answer)
- expect { }.to throw_symbol(:error)
- expect { }.to trigger_exception(ErrorClass)
- expect { }.to emit_error(ErrorClass)
Correct answer: expect { }.to raise_error(ErrorClass)
`raise_error` accepts an exception class and optionally a message to assert that a block raises that specific error.
Question 2: In Rails system tests using Capybara, what method waits for an element to appear before interacting with it?
- find (Correct answer)
- locate
- seek
- poll
Correct answer: find
Capybara's `find` method automatically waits up to the configured `default_max_wait_time` for an element to become present.
Question 3: Which tool is commonly used to measure Rails test coverage and identify untested code paths?
- SimpleCov (Correct answer)
- Coveralls only
- RuboCop
- Brakeman
Correct answer: SimpleCov
SimpleCov is the standard Ruby gem that instruments code and reports line/branch coverage percentages after your test suite runs.
Question 4: What is the purpose of `VCR` gem in Rails testing?
- Record and replay HTTP interactions to make tests deterministic (Correct answer)
- Mock ActiveRecord queries in unit tests
- Capture browser screenshots during system tests
- Profile slow database queries
Correct answer: Record and replay HTTP interactions to make tests deterministic
VCR records real HTTP interactions into cassette files and replays them in future test runs, eliminating external network calls.
Question 5: When using FactoryBot, what does `build_stubbed` do differently than `build`?
- Creates an in-memory object with stubbed persistence methods without hitting the database (Correct answer)
- Saves the record and stubs all associations
- Builds the object and creates a database record for associations only
- Stubs the factory itself to return nil
Correct answer: Creates an in-memory object with stubbed persistence methods without hitting the database
`build_stubbed` creates a fake-persisted object with a fake id, stubbing `persisted?` and `save` so no database interaction occurs.
Question 6: Which RSpec configuration option re-runs previously failing tests first?
- --order failed-first (Correct answer)
- --bisect
- --fail-fast
- --profile
Correct answer: --order failed-first
`--order failed-first` uses `.rspec_status` to run previously failing examples before others, giving faster feedback on regressions.
Question 7: What does the `database_cleaner` gem's `:truncation` strategy do compared to `:transaction`?
- Truncates all tables after each test instead of wrapping tests in a rolled-back transaction (Correct answer)
- Wraps each test in a transaction and rolls it back
- Drops and recreates the schema after every test
- Clears only the test fixtures table
Correct answer: Truncates all tables after each test instead of wrapping tests in a rolled-back transaction
`:truncation` issues TRUNCATE SQL statements after each test, required when tests use multiple threads or feature tests that bypass transactions.
Which RSpec matcher should you use to verify that a method raises a specific exception class?