RailsプロジェクトにRspec環境を構築する - 2020年4月
Gemfileに必要するgemを追加してbundle
group :test do gem 'rspec-rails' gem 'factory_girl_rails' gem 'rspec-collection_matchers' gem 'faker' gem 'email_spec' gem 'capybara' gem 'webdrivers' gem 'timecop' gem 'simplecov', require: false end
追加する部分のみ抜粋
bundle install
現時点で追加されるrspec-railsのバージョンは4.0.0
rails generate rspec:install
以下のファイルが作成される
create .rspec create spec create spec/spec_helper.rb create spec/rails_helper.rb
helperをいい感じに変更(diffだけ
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 00345af..35c3632 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -6,6 +6,9 @@ require File.expand_path('../config/environment', __dir__) abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! +require 'webdrivers' +require 'email_spec' +require 'email_spec/rspec' # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are @@ -20,7 +23,7 @@ require 'rspec/rails' # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # -# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } +Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } # Checks for pending migrations and applies them before tests are run. # If you are not using ActiveRecord, you can remove these lines. @@ -32,7 +35,7 @@ rescue ActiveRecord::PendingMigrationError => e end RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures - config.fixture_path = "#{::Rails.root}/spec/fixtures" + # config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ce33d66..81868a4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,10 @@ # it. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +require 'capybara/rspec' +require 'json_matchers/rspec' +JsonMatchers.schema_root = 'spec/support/api/schemas' + RSpec.configure do |config| # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb new file mode 100644 index 0000000..3dfc3d1 --- /dev/null +++ b/spec/support/capybara.rb @@ -0,0 +1,16 @@ +RSpec.configure do |config| + config.before(:each, type: :system) do + driven_by :rack_test + end + + config.before(:each, type: :system, js: true) do + driven_by(:selenium, using: :headless_chrome, screen_size: [1920, 5120]) do |options| + options.add_argument('--lang=ja') + options.add_argument('--no-sandbox')