Week 14 Building an API Part 2 - Code-the-Dream-School/rails-guidebook GitHub Wiki

Week Topic Learning Objectives Key Resources
14 Building an API Part 2
  • Integrate Rspec and Swagger into the Rails Application
  • Build Rspec tests for logon and CRUD operations
  • Understand how Swagger is used to document REST APIs
  • Build Swagger definitions from Rspec tests
  • Practice with the Swagger UI
Lesson Materials - See Coding Assignment

Coding Assignment

Lesson Outline

In this lesson, students will:

  1. Set Up Testing and Documentation Tools

    • Install and configure RSpec and Swagger for API testing and documentation.
    • Add necessary gems to the Gemfile and install them using bundle install.
    • Generate RSpec and RSwag configuration files.
  2. Create FactoryBot Factories

    • Define factories for User, Member, and Fact models using FactoryBot.
  3. Write RSpec Tests for API Endpoints

    • Create RSpec tests for user registration, session management, member, and fact controllers.
    • Use Swagger specifications to document the API within the tests.
  4. Generate Swagger Documentation

    • Configure Swagger to generate API documentation.
    • Verify and test the generated Swagger UI.
  5. Submit and Propose Final Project

    • Ensure all tests pass and submit the work on GitHub.
    • Start planning the final project and submit a proposal using the provided rubric.

Checklist

  • Branch Creation

    • Create and switch to the lesson13 branch from the lesson12 branch.
  • Gem Installation

    • Add rspec-rails, rexml, and rswag gems to the Gemfile.
    • Add factory_bot_rails, faker, and rails-controller-testing to the :test group in the Gemfile.
    • Run bundle install.
    • Generate RSpec and RSwag configuration with bin/rails generate rspec:install and bin/rails generate rswag:install.
  • FactoryBot Factories

    • Create spec/factories/users.rb with user factory.
    • Create spec/factories/members.rb with member factory.
    • Create spec/factories/facts.rb with fact factory.
  • RSpec Tests

    • Update spec/rails_helper.rb to include Devise test helpers.
    • Create spec/requests/registrations_spec.rb for user registration.
    • Create spec/requests/sessions_spec.rb for session management.
    • Create spec/requests/api/v1/members_spec.rb for member API.
    • Create spec/requests/api/v1/facts_spec.rb for fact API.
  • Swagger Configuration

    • Edit spec/swagger_helper.rb to set up Swagger with correct URL and authentication.
    • Run bundle exec rake rswag:specs:swaggerize to generate Swagger documentation.
  • Testing and Verification

    • Verify that all RSpec tests pass.
    • Access Swagger UI at http://localhost:3000/api-docs and test the API endpoints.
    • Handle CSRF token issues for POST, PUT, and DELETE requests in Swagger UI.
  • Submission

    • Add, commit, and push changes to the lesson13 branch.
    • Create a pull request on GitHub.
  • Final Project Proposal

    • Review the final project rubric.
    • Develop and submit a final project proposal using the Homework Assignment Submission Form.
    • Communicate with the mentor via Slack if needed.
⚠️ **GitHub.com Fallback** ⚠️