Ruby on Rails Ruby on Rails Testing, Security & API Development 1 — Questions and Answers
Question 1: What testing framework does Rails include by default for unit and integration tests?
- RSpec
- Minitest (Correct answer)
- Cucumber
- Jasmine
Correct answer: Minitest
Rails ships with Minitest as its default testing framework, providing test cases for models, controllers, and integration scenarios.
Question 2: What is the purpose of a Rails 'system test'?
- Testing database migrations
- End-to-end browser testing that simulates real user interactions (Correct answer)
- Unit testing model validations
- Testing API JSON responses
Correct answer: End-to-end browser testing that simulates real user interactions
System tests in Rails use a real browser (via Capybara) to test full user workflows from clicking links to form submissions.
Question 3: What does CSRF protection do in a Rails application?
- Encrypts database passwords
- Prevents cross-site request forgery by validating an authenticity token with form submissions (Correct answer)
- Sanitizes SQL queries
- Blocks XSS attacks in views
Correct answer: Prevents cross-site request forgery by validating an authenticity token with form submissions
Rails CSRF protection embeds a unique token in forms and validates it on non-GET requests to prevent malicious cross-site form submissions.
Question 4: In Rails API development, which method is used to render a JSON response from a controller?
- show json:
- output :json
- render json: @object (Correct answer)
- respond json: @object
Correct answer: render json: @object
`render json: @object` serializes the Ruby object to JSON and sends it as the HTTP response with the appropriate content-type.
Question 5: What is Strong Parameters in Rails and why is it used?
- A way to define required database columns
- A security feature that requires explicitly permitting which parameters can be mass-assigned (Correct answer)
- A method for validating parameter types
- A configuration for stricter SQL queries
Correct answer: A security feature that requires explicitly permitting which parameters can be mass-assigned
Strong Parameters (via `params.require(...).permit(...)`) prevents mass-assignment vulnerabilities by whitelisting which request parameters can update model attributes.
Question 6: Which Rails environment is used when running `rails test` by default?
- development
- staging
- production
- test (Correct answer)
Correct answer: test
The test environment uses a separate database and configuration to isolate test runs from development and production data.
What testing framework does Rails include by default for unit and integration tests?