Setting up Rails 6 API and RSpec Test Environment

Setup Rails API

This is going to be an API only application so we don’t need some of the helpers of a default Rails application.

>rails new your_application --api --skip-test
  • Configure your application to start with a more limited set of middleware than normal. Specifically, it will not include any middleware primarily useful for browser applications (like cookies support) by default.
  • Make ApplicationController inherit from ActionController::API instead of ActionController::Base. As with middleware, this will leave out any Action Controller modules that provide functionalities primarily used by browser applications.
  • Configure the generators to skip generating views, helpers, and assets when you generate a new resource.

Debugging Tools

I like using Pry as runtime developer console for Rails so I add gem package called pry-rails to the Gemfile in addition to that to boost its debugging potential I also add the pry-byebug gem. It adds next, step, finish, continueand breakcommands to pry using byebug.

Enable CORS

Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell a browser to let a web application running at one origin (domain) have permission to access selected resources from a server at a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, and port) than its own origin.

gem 'rack-cors', require: 'rack/cors'

Setup Test Tools

Now we can start setting up the test environment, for this we need to add three gem packages named; rspec-rails, shoulda-matchers, factory_bot_rails.

> rails generate rspec:install



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Erbay

Ali Erbay

Full Stack Developer BDD/TDD || Ruby on Rails || React || React Native || NodeJS || Express