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 |
|
Lesson Materials Coding Assignment |
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.
- 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.
-
Branch Setup
- Create a new branch
lesson9from the existinglesson8branch.
- Create a new branch
-
Gemfile Updates
- Add the following gems to the
:development, :testsection of the Gemfile:rspec-railsfactory_bot_railsfakerrails-controller-testingrexml
- Run
bundle install.
- Add the following gems to the
-
RSpec Setup
- Run
bin/rails generate rspec:install. - Run
bin/rails db:migrate db:test:prepare.
- Run
-
Generate Test Files
- Run
bin/rails generate rspec:model Customer. - Run
bin/rails generate rspec:request Customers.
- Run
-
Model Test Implementation
- Edit
spec/models/customer_spec.rband add the provided RSpec tests. - Implement missing
itblocks for model validations. - Run
bundle exec rspecand verify all model tests pass.
- Edit
-
Request Test Configuration
- Edit
spec/factories/customers.rbto define the customer factory using Faker. - Edit
spec/requests/customers_spec.rband add the provided request tests. - Implement missing
itblocks for request tests.
- Edit
-
Run Request Tests
- Run
bundle exec rspecand ensure all request tests pass. - Verify and fix any failing tests due to configuration or missing functionality.
- Run
-
Controller Testing
- Edit
app/controllers/customers_controller.rbto uncomment necessary lines. - Confirm that the tests fail if incorrect lines are commented and pass after corrections.
- Edit
-
Final Review
- Ensure all tests are implemented and passing.
- Add, commit, and push the
lesson9branch to GitHub. - Create a pull request for the
lesson9branch.
-
Submission
- Submit the pull request for review.