Week 10 Rails Testing with RSPEC - Code-the-Dream-School/rails-guidebook GitHub Wiki

Week Topic Learning Objectives Key Resources
10 Rails Testing with RSPEC
  • Understand why automated testing is important
  • Learn to generate random data for use in testing
  • Acquire familiarity with Rspec automated tests
  • Write model and request tests using Rspec
Lesson Materials

Coding Assignment

Lesson 9 Outline

Overview

In this lesson, students will set up and complete automated testing for the customer-order application using RSpec and associated gems. They will create and configure test files for both model and request testing, and implement tests to ensure that the customer model and associated controllers are functioning correctly.

Key Learning Objectives

  • Add RSpec and related gems: Update the Gemfile and install necessary gems for testing.
  • Generate RSpec configuration and test files: Set up the RSpec configuration, generate model and request test files.
  • Write and run model tests: Implement and execute tests for the Customer model to validate attributes and methods.
  • Configure request tests with FactoryBot and Faker: Set up request tests for CRUD operations and use FactoryBot and Faker to create test data.
  • Complete and run request tests: Write missing request test cases and validate controller actions.
  • Submit the work: Commit changes to GitHub and create a pull request.

Checklist

  1. Branch Setup

    • Create a new branch lesson9 from the existing lesson8 branch.
  2. Gemfile Updates

    • Add the following gems to the :development, :test section of the Gemfile:
      • rspec-rails
      • factory_bot_rails
      • faker
      • rails-controller-testing
      • rexml
    • Run bundle install.
  3. RSpec Setup

    • Run bin/rails generate rspec:install.
    • Run bin/rails db:migrate db:test:prepare.
  4. Generate Test Files

    • Run bin/rails generate rspec:model Customer.
    • Run bin/rails generate rspec:request Customers.
  5. Model Test Implementation

    • Edit spec/models/customer_spec.rb and add the provided RSpec tests.
    • Implement missing it blocks for model validations.
    • Run bundle exec rspec and verify all model tests pass.
  6. Request Test Configuration

    • Edit spec/factories/customers.rb to define the customer factory using Faker.
    • Edit spec/requests/customers_spec.rb and add the provided request tests.
    • Implement missing it blocks for request tests.
  7. Run Request Tests

    • Run bundle exec rspec and ensure all request tests pass.
    • Verify and fix any failing tests due to configuration or missing functionality.
  8. Controller Testing

    • Edit app/controllers/customers_controller.rb to uncomment necessary lines.
    • Confirm that the tests fail if incorrect lines are commented and pass after corrections.
  9. Final Review

    • Ensure all tests are implemented and passing.
    • Add, commit, and push the lesson9 branch to GitHub.
    • Create a pull request for the lesson9 branch.
  10. Submission

    • Submit the pull request for review.
⚠️ **GitHub.com Fallback** ⚠️