Ruby on Rails Quality Control & Assurance 3 — Questions and Answers
Question 1: In Minitest, which assertion checks that two objects are the same object instance (identity, not equality)?
- assert_same (Correct answer)
- assert_equal
- assert_identical
- assert_object_id
Correct answer: assert_same
`assert_same` uses `equal?` (object identity via `object_id`) rather than `==` to verify two variables reference the exact same object.
Question 2: Which Rails test helper method allows you to test controller actions that require authentication by simulating a logged-in user?
- sign_in (Devise test helper) (Correct answer)
- authenticate_request
- set_session(:user_id)
- login_as_admin
Correct answer: sign_in (Devise test helper)
Devise provides `sign_in` as a test helper that populates Warden's test env, bypassing actual authentication for controller and request specs.
Question 3: What is the role of `shared_examples` in RSpec?
- Define reusable groups of examples that can be included in multiple describe blocks (Correct answer)
- Share test data between different spec files
- Run the same example in parallel across multiple threads
- Import examples from external gem specifications
Correct answer: Define reusable groups of examples that can be included in multiple describe blocks
`shared_examples` lets you define a named group of examples once and include it with `it_behaves_like` wherever the same behavior must be verified.
Question 4: Which RuboCop cop category enforces Rails-specific style rules like using `presence` instead of `blank?` checks?
- RuboCop-Rails (Correct answer)
- RuboCop-Performance
- RuboCop-RSpec
- RuboCop-Minitest
Correct answer: RuboCop-Rails
The `rubocop-rails` extension adds cops specific to Rails idioms, such as `Rails/Blank` and `Rails/Present`, on top of core RuboCop.
Question 5: In a Rails request spec, which HTTP method helper would you use to test a JSON API endpoint that creates a resource?
- post path, params: data, headers: { 'Content-Type' => 'application/json' } (Correct answer)
- create path, body: data
- send_request(:post, path, data)
- api_call(:post, path)
Correct answer: post path, params: data, headers: { 'Content-Type' => 'application/json' }
Rails integration test helpers (`get`, `post`, `patch`, `delete`) accept a `params` hash and `headers` hash for full HTTP simulation.
Question 6: What does Brakeman's output 'Confidence: High' indicate in a security scan report?
- The vulnerability is almost certainly a real issue requiring immediate attention (Correct answer)
- The finding is a false positive
- The scan only partially analyzed the code path
- High confidence means the fix has already been applied
Correct answer: The vulnerability is almost certainly a real issue requiring immediate attention
Brakeman rates findings by confidence (High/Medium/Weak); High means the static analysis found a definitive vulnerability pattern with little ambiguity.
Question 7: Which Rails test type is best suited for verifying that background jobs enqueue correctly without actually executing the job?
- Use `assert_enqueued_with` in an ActiveJob test with `queue_adapter :test` (Correct answer)
- Use a system test with Capybara to click the trigger button
- Use a model unit test checking the job class name
- Use a controller test calling `perform_now` directly
Correct answer: Use `assert_enqueued_with` in an ActiveJob test with `queue_adapter :test`
`queue_adapter :test` captures enqueued jobs in memory, and `assert_enqueued_with` verifies the job class and arguments without side effects.
In Minitest, which assertion checks that two objects are the same object instance (identity, not equality)?