【Ruby on Rails】RSpecとFactoryBotとShoulda Matchersを導入メモ

Ruby on Rails

Updated by Yuto Yasunaga at 2021/01/16 09:12

RSpec設定

Gemfile
group :development, :test do
  gem 'rspec-rails'
end
rails g rspec:install
.rspec
--require spec_helper
--format documentation
config/application.rb
config.generators do |g|
  g.test_framework :rspec,
                   fixtures: false,
                   controller_specs: false,
                   view_specs: false,
                   helper_specs: false,
                   decorator_specs: false,
                   routing_specs: false

Spring設定

Gemfile
group :development, :test do
  gem 'spring-commands-rspec'
end
spring binstub rspec

FactoryBot設定

Gemfile
group :development, :test do
  gem "factory_bot_rails"
end
spec/support/factory_bot.rb
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
spec/rails_helper.rb
require "support/factory_bot"

Shoulda Matcher設定

Gemfile
group :test do
  gem 'shoulda-matchers'
end
spec/support/shoulda_matchers.rb
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
spec/rails_helper.rb
require "support/shoulda_matchers"

FFaker設定

Gemfile
group :development, :test do 
  gem 'ffaker'
end

Fuubar設定

Gemfile
group :test do
  gem 'fuubar'
end
.rspec
--format Fuubar
--color

Back