0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

サポートモジュールを使用したテストコードのまとめ

Posted at

アウトプット

サポートモジュールとは、RSpecに用意されている、メソッド等をまとめる機能。

実装の手順

① specディレクトリ配下にsupportディレクトリを作成し、その配下にメソッド名.rbのファイルを作成する

(例)spec/support/sign_in_support.rb
module SignInSupport
  def sign_in(user)
    visit new_user_session_path
    fill_in 'Email', with: user.email
    fill_in 'Password', with: user.password
    find('input[name="commit"]').click
    expect(current_path).to eq(root_path)
  end
end

② サポートモジュールを読み込める設定をする
Rspecを用いてRailsの機能をテストするときに、共通の設定を書いておくファイルspec/rails_helper.rb
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }部分のコメントアウトを外す

spec/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
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!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# 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 }

同ファイルのRSpec.configureの中にモジュール名を記述

RSpec.configure do |config|
  config.include SignInSupport
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

# 以下省略

③結合テストコードの該当部分を、メソッド名に置き換える

spec/system/ファイル名.rb
require 'rails_helper'

RSpec.describe 'データ投稿', type: :system do
  before do
    @user = FactoryBot.create(:user)
    @data_text = Faker::Lorem.sentence
    @data_image = Faker::Lorem.sentence
  end
  context 'データ投稿ができるとき'do
    it 'ログインしたユーザーは新規投稿できる' do
      # ログインする
      sign_in(@user)
  # 以下省略

学んだこと

MVCにおけるbefore_actionメソッドや、
テストコードにおけるbeforeメソッドFactoryBotのように、
結合テストコードにおいても記述量を減らす機能が、Railsに用意されている。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?