Installing Rspec and Capybara on Rails 5

Installing Rspec and Capybara on Rails 5. Some simple instructions

Create a new rails project

rails new project-name -T

Create a new rails project

Cd into the project and make sure you can run the rails server.

cd project-name
rails s

After verifying the server is running kill the server with a Ctrl+C in the terminal

In the gem file add rspec-rails gem under group development test

 
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'rspec-rails', '3.5.1'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

In the gem file create a group :test do to add capybara

 
group :test do
  gem 'capybara', '2.7.1'
end

In the terminal install bundle install

 
bundle install

In the terminal install rspec

 
rails g rspec:install

In the terminal add binstubs

 
bundle binstubs rspec-core

Open the spec folder and scroll to the bottom of rails_helper.rb and add the following

config.backtrace_exclusion_patterns = [
  /\/lib\d*\/ruby\//,
  /bin\//,
  /gems/,
  /spec\/spec_helper\.rb/,
  /lib\/rspec\/(core|expectations|matchers|mocks)/
]

Return to the gem folder and Guard to the group development section

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'guard', '~>2.14.0'
  gem 'guard-rspec', '~> 4.7.2'
end

In the terminal install bundle install

 
bundle install

In the terminal initialize Guard

 
guard init

In the Guard file change

 
guard :rspec, cmd: "bundle exec rspec" do

to

 
guard :rspec, cmd: "bin/rspec" do

In the Guard file above the comment # Rails config changes add the following

 
watch(%r{^app/models/(.+)\.rb$}) { |m| "spec/features/#{m[1]}s" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| "spec/features/#{m[1]}" }
watch(rails.routes)          { "#{rspec.spec_dir}" }

 

In the Guard file above the comment # Capybara features spec add the following

watch(rails.view_dirs) { |m| "spec/features/#{m[1]}" }
watch(%r{^app/views/layout/application.html.erb$}) { "spec/features" }

Save the Guard file

In the terminal run Rspec

 
rspec spec

Or … in the terminal run Rspec in a way that’s more specific and optimized

 
rspec spec/features/homepage_spec.rb

 

Leave a comment