Set up RSpec to save you time
As developers, we have to resolve bugs in our software. And the sooner we find them out, the better our lives are.
Yes, I hear your voice. It’s testing. You are brilliant.
In this blog post, we will explore how we can set up testing for our Ruby on Rails projects with RSpec. Why RoR? Well, because I’m familiar with it.
1. The Gemfile
Enough long talk, let’s install the dependencies in your Gemfile.
# Gemfile
group :development, :test do
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'database_cleaner-active_record'
gem 'shoulda-matchers'
gem 'guard-rspec'
endThen just:
# Terminal
$ bundle installLet me have some words about the above libraries:
rspec-railsis for, hmmm, RSpec itself.factory_bot_railsis used to manage testing data which guys often call themfixtures.
2. Initialize
# Terminal
$ rails generate rspec:install
$ guard init rspec3. Configuration
# rails_helper.rb
RSpec.configure do |config|
...
config.include FactoryBot::Syntax::Methods
config.include RSpec::Rails::RequestExampleGroup, type: :request, file_path: /spec\/api/
config.include ActiveSupport::Testing::TimeHelpers
end4. Profit
# Terminal
$ guardAnd that’s it. You are good to go now. Cheers!