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.

Rspecの処理を共通化させる

Last updated at Posted at 2022-09-19
  • specフォルダ直下にsupportフォルダを作る
  • 共通化したい処理を書いたモジュールを追記
json_api_helper
module JsonApiHelpers
  def json
    JSON.parse(response.body)
  end
  def json_data
    json["data"]
  end
end
  • rails_helper.rbの23行目をコメントアウトする。これをコメントアウトすると、spec/supportに入っているファイルをループで全てrequireする。
rails_helper.rb
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
  • rails_helper.rbの下記のブロックにincludeしたいファイルを追記。spec_helper.rbの似たブロックと間違わないように!!!
rails_helper.rb
RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.include FactoryBot::Syntax::Methods
  config.include JsonApiHelpers

これで処理が共通化されました!

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?