DEV Community

Cover image for Setup RSpec on a fresh Rails 7 project
Adrian Valenzuela
Adrian Valenzuela

Posted on • Updated on

Setup RSpec on a fresh Rails 7 project

Photo by Jonathan Bean on Unsplash

Installing RSpec, FactoryBot, Faker gems and configuring for testing environment.

Using Ruby 3.0.1 + Rails 7.0.0 at the time of writing this article

Let's get right to it...

Start a fresh project

rails new projectname -d postgresql
cd projectname
git add -A
git commit -m "initial commit"

rails db:create
rails s
Enter fullscreen mode Exit fullscreen mode

Install gems

git checkout -b rspec
Enter fullscreen mode Exit fullscreen mode

In Gemfile add...

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem "debug", platforms: %i[ mri mingw x64_mingw ]
  gem "rspec-rails"
  gem "factory_bot_rails"
  gem "faker"
end
Enter fullscreen mode Exit fullscreen mode

Back in terminal/command line...

bundle install
rails g rspec:install
Enter fullscreen mode Exit fullscreen mode

Configure gems

Create 'support' directory in 'spec' directory and create two files

spec/support/factory_bot.rb
spec/support/chrome.rb
Enter fullscreen mode Exit fullscreen mode

And then one file in the spec directory for factories.

spec/factories.rb
Enter fullscreen mode Exit fullscreen mode

Configure FactoryBot

# spec/support/factory_bot.rb

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
Enter fullscreen mode Exit fullscreen mode

Configure driver

# spec/support/chrome.rb

RSpec.configure do |config|
  config.before(:each, type: :system) do
    if ENV["SHOW_BROWSER"] == "true"
      driven_by :selenium_chrome
    else
      driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Require support files in rails_helper.rb

# spec/rails_helper.rb

require_relative 'support/factory_bot'
require_relative 'support/chrome'
Enter fullscreen mode Exit fullscreen mode

When you generate a User model (or any model) RSpec will generate a factory for you in your factories.rb file.
You can modify it to look like this.

# spec/factories.rb

FactoryBot.define do
  factory(:user) do
    email { Faker::Internet.email }
    password { Faker::Internet.password }
  end
end
Enter fullscreen mode Exit fullscreen mode

it "should run a test" do...

Run rspec command in the terminal and you should see it run and output some green text.

-> rspec
No examples found.

Finished in 0.00038 seconds (files took 0.94577 seconds to load)
0 examples, 0 failures
Enter fullscreen mode Exit fullscreen mode

Finish up

git add -A
git commit -m "add rspec"
git checkout main
git merge rspec
git branch -D rspec
git push # assuming you have a repo online somewhere
Enter fullscreen mode Exit fullscreen mode

Latest comments (7)

Collapse
 
gustavobk profile image
Gustavo Burckhardt • Edited

thanks for this, but for me it shows" invoke factory_bot
File unchanged! The supplied flag value not found! spec/factories.rb"
every tirme I try to create a model/scaffold.

Collapse
 
adrianvalenz profile image
Adrian Valenzuela

I need to update this post. I remember now that being an issue...

Just totally forget and remove that file: spec/factories.rb ...and now when you generate a model or scaffold it will run smoothly (I hope!) ^_^

If you created a model like bin/rails g model cake name:string before you deleted that spec/factories.rb file...then you can either start over and first destroy your model (bin/rails d model cake ...note the d flag and you don't have to pass in your attributes) and then just generate it again...or if you are in too deep and already began working with your model you can just generate the factory for your model like this: bin/rails g factory_bot:model Cake name:string. I believe do you do have to pass in your attributes or you can manually add them after it creates the factory file for you.

Collapse
 
gustavobk profile image
Gustavo Burckhardt

yeah! that solved the issue. :D
Thanks!

Collapse
 
ukrmaxim profile image
ukrmaxim

Thank you. Good job!

Collapse
 
pas256 profile image
Peter Sankauskas

This is fantastic, thank you!

I noticed in test/test_helper.rb there is a line to help speed up testing. How did you do the equivalent of:

parallelize(workers: :number_of_processors)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
adrianvalenz profile image
Adrian Valenzuela

It looks like there is a gem and a video tutorial that can assist with that: github.com/grosser/parallel_tests and railscasts.com/episodes/413-fast-t... ..

From what I understand it's not built into RSpec which you can refer to this GitHub issue for starters: github.com/rspec/rspec-rails/issue...

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Now you can also use Ruby 3.1 with Rails 7.0.1.