Ruby on Rails Quality Control & Assurance 5 — Questions and Answers
Question 1: Which approach correctly stubs an instance method on a specific object in RSpec without affecting other instances?
- allow(instance).to receive(:method_name).and_return(value) (Correct answer)
- stub_method(instance, :method_name, value)
- instance.stub(:method_name) { value }
- RSpec.stub_on(instance, :method_name, value)
Correct answer: allow(instance).to receive(:method_name).and_return(value)
`allow(obj).to receive` sets up a message expectation on that specific instance only, leaving other instances of the class unaffected.
Question 2: In Rails, what does `assert_difference('Model.count', 2)` verify in a test block?
- That exactly 2 new records were added to the database during the block's execution (Correct answer)
- That the model's count attribute increased by 2
- That 2 records were deleted
- That Model.count equals 2 after the block
Correct answer: That exactly 2 new records were added to the database during the block's execution
`assert_difference` evaluates the expression before and after the block and asserts the difference equals the given delta (default 1, here 2).
Question 3: What is the purpose of parallel test execution in Rails via `parallelize(workers: :number_of_processors)`?
- Split the test suite across multiple processes to reduce total wall-clock run time (Correct answer)
- Run the same test simultaneously to detect race conditions
- Distribute tests across multiple machines via a CI server
- Execute each test file in a separate Ruby process for isolation
Correct answer: Split the test suite across multiple processes to reduce total wall-clock run time
Rails' `parallelize` forks the test suite into N worker processes each with its own test database, dramatically reducing total test time on multi-core machines.
Question 4: Which security linting concern would Brakeman flag in a Rails view that renders `<%= params[:name] %>`?
- Cross-site scripting (XSS) via unfiltered user input (Correct answer)
- SQL injection from the params hash
- CSRF token exposure
- Server-side template injection
Correct answer: Cross-site scripting (XSS) via unfiltered user input
Even though Rails auto-escapes ERB output, Brakeman flags direct `params` rendering as a potential XSS vector because the data originates from untrusted user input.
Question 5: In RSpec, what is the difference between `before(:each)` and `before(:all)` hooks?
- `before(:each)` runs before every example; `before(:all)` runs once before all examples in the group and shares state (Correct answer)
- `before(:all)` runs after all examples; `before(:each)` runs only before the first
- `before(:each)` and `before(:all)` are identical aliases
- `before(:all)` requires a database transaction that persists across examples
Correct answer: `before(:each)` runs before every example; `before(:all)` runs once before all examples in the group and shares state
`before(:all)` (also `:context`) runs the block once, with shared mutable state potentially causing order-dependent failures between examples.
Question 6: Which gem provides contract testing in Ruby, ensuring that API consumers and producers honor a shared contract?
- Pact (Correct answer)
- Apipie
- Grape
- Swagger-blocks
Correct answer: Pact
Pact implements consumer-driven contract testing where the consumer defines expected interactions and the provider verifies it honors them independently.
Question 7: When using `freeze_time` (or `travel_to`) in Rails tests, what problem does it solve?
- Makes time-dependent logic deterministic by fixing `Time.current` to a specific moment (Correct answer)
- Prevents database timestamps from being updated during tests
- Disables all background jobs that use scheduled timestamps
- Freezes the test transaction so time-based callbacks don't fire
Correct answer: Makes time-dependent logic deterministic by fixing `Time.current` to a specific moment
`travel_to` (ActiveSupport) or `freeze_time` (timecop/activesupport) stubs `Time.now`/`Time.current` so tests relying on relative time comparisons produce consistent results.
Which approach correctly stubs an instance method on a specific object in RSpec without affecting other instances?