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

Ruby on Rails

Posted by Yuto Yasunaga at 2020-08-23 20:07:59 +0900

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, 
    view_specs: false, 
    helper_specs: false, 
    routing_specs: false
end

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
config/application.rb
config.generators do |g| 
  g.test_framework :rspec,
    # fixtures: false, 
    view_specs: false, 
    helper_specs: false, 
    routing_specs: false
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"

Back