Add RSpec related gems to gemfile:
group :development, :test do gem 'rspec-rails' end group :test do gem "capybara" end
Capybara is a framework for testing web applications; allows to navigate your app programmatically.
Install your RSpec gems: bundle install
Generate RSpec templates:
generate rspec: rails g rspec:install
To view all available generators: rails g
Since RSpec uses test database, run your migrations for test environment:
rails db:migrate RAILS_ENV=test
If this is the first run, may need to set up the database by running RAILS_ENV=test rake db:reset
To run all specs: rspec
To add formatting: rspec -format doc (or use shortcut: rspec -f doc)
To always use the same formatting, add -format doc to .rspec file.
To use helper files for RSpecs
Uncomment this line in rails_helper.rb:
Dir[Rails.root.join(‘spec/support/**/*.rb’)].each { |f| require f }
And place all your helper methods inside of spec/support directory.
Sample RSpec file:
require 'rails_helper' describe 'Navigating movies' do it 'allows navigation from the detail page to the listing page' do movie = Movie.create(movie_attributes) visit movie_url(movie) click_link 'All Movies' expect(current_path).to eq(root_path) end