Ruby /

Testing

Common Ruby & Rails Test Types:

  • Unit Tests: Test individual methods, models, or classes (POROs - Plain Old Ruby Objects).
  • Model Specs (RSpec) / Model Tests (Rails): Validates database interactions and validations.
  • Controller Specs (RSpec) / Functional Tests (Rails): Ensures actions return the correct response, templates, or redirects.
  • Request Specs (RSpec) / Integration Tests (Rails): Tests HTTP requests and responses, focusing on API endpoints or full request flows.
  • System Specs (RSpec) / System Tests (Rails): Simulates user interactions in a browser, usually powered by Capybara.
  • View/Helper Specs: Test specific UI components or helper methods.
  • Mailer/Job Specs: Verify email delivery and background job processing. 

Key Tools & Frameworks:

  • RSpec: A BDD-focused framework designed for readability and expressing expected behavior.
  • Minitest: The standard library testing tool included with Ruby, known for being lightweight and fast.
  • Capybara: Used in conjunction with RSpec or Minitest to simulate user browser behavior.
  • FactoryBot: Frequently used to generate test data.
  • Database Cleaner: Often used to manage database state between tests. 

Best Practices:

  • TDD/BDD: Adopting Test-Driven Development (writing tests first) or Behavior-Driven Development.
  • Fast Execution: Keeping unit tests quick to run.
  • Isolation: Ensuring tests do not depend on each other