Skip to content
mengfung.com
writing

New Rails 6.0.3.2 with Rspec, React, and PostgreSQL Project

 ·  2 min read

I use Bamboo for my CI/CD so some of this you might not need l will state so in comments.

Create the new project:

<code>rails new project_name --webpack=react -d=postgresql -T</code>

-T tells rails to ignore installing a testing framework.

Setup your database by entering info into config/database.yml and create.

<code>rails db:create
rails db:create RAILS_ENV=test 
# If using encrypted credentials I use VI
EDITOR='vi' rails credentials:edit</code>

And I edit config/database.yml accordingly.

Add Rspec to your Gemfile:

<code>gem 'rspec-rails'</code>

Here are some of my other gem additions for development and testing:

<code>group :development, :test do
gem 'annotate'
gem 'factory_bot_rails'
gem 'faker'
gem 'pry-rails'
gem 'rubocop'
gem 'rubocop-faker'
gem 'simplecov'
gem 'simplecov-bamboo' # for bamboo
gem 'simplecov-summary'
gem 'vcr'
gem 'webmock'
...
end
group :development do
gem 'activerecord-explainer'
gem 'brakeman'
gem 'bullet'
gem 'bundler-audit'
gem 'rubycritic', require: false
...
end</code>

Commands I often run:

rubocop -a // auto correct and check for style errors
brakeman // check for security issues
rubycritic // looks for code smells and complexity
bundler-audit // audit your gems

Install Rspec:

<code>rails g rspec:install</code>

file location: spec/rails_helper.rb

Setup Rspec:

<code>require 'simplecov'
require 'simplecov-bamboo' # for bamboo
SimpleCov.start 'rails' do
add_group 'Workflows', 'app/workflows'
add_group 'Workers', 'app/workers'
add_group 'Service Objects', 'app/service_objects'
end
require 'spec_helper'
...
require 'rspec/rails'
require 'capybara/rspec'
require 'sidekiq/testing'
require 'vcr'
require 'webmock/rspec'
...
config.infer_spec_type_from_file_location!
config.after(:suite) do
SimpleCov.at_exit do
SimpleCov::Formatter::SummaryFormatter.new.format(SimpleCov.result)
SimpleCov::Formatter::HTMLFormatter.new.format(SimpleCov.result)
SimpleCov::Formatter::BambooFormatter.new.format(SimpleCov.result)              
end
end</code>

Running Test:

<code>rails spec</code>

Notice that coverage folder is created.

Generate First Page:

<code>rails g controller Pages index</code>

Modify Routes:

File location: config/routes.rb

<code>Rails.application.routes.draw do
root 'pages#index'
match '*path', to: 'pages#index', via: :all
end</code>

Running webpacker server for live updates.

./bin/webpacker-dev-server

Update views for React:

File location: app/views/pages/index.html.erb

<code>&lt;div>&lt;/div></code>

File location: app/views/layouts/application.html.erb

<code>&lt;%= javascript_pack_tag 'hello_react' %></code>

This should have Hello React! displayed and if you change the jsx file it should live update.