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
lesson9
from the existinglesson8
branch.
- Create a new branch
-
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
.
- 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.rb
and add the provided RSpec tests. - Implement missing
it
blocks for model validations. - Run
bundle exec rspec
and verify all model tests pass.
- Edit
-
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.
- Edit
-
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.
- Run
-
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.
- Edit
-
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.
-
Submission
- Submit the pull request for review.