LoginSignup
1
0

More than 1 year has passed since last update.

rakeファイルをRSpecでテストする

Posted at

環境

Ruby 3.0.2
Rails 6.1.4.1

設定

spec/rails_helper.rb
...
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
...
spec/rake_helper.rb
require 'rake'
require 'rails_helper'

RSpec.configure do |config|
  config.before(:suite) do
    Rails.application.load_tasks
  end

  config.before(:each) do
    Rake.application.tasks.each(&:reenable)
  end
end

spec下にファイルを作成し、いつものようにテストを書いていく。

spec/lib/tasks/XXXXX_spec.rb
require 'rake_helper'

describe 'XXXXX:XXXXXXX' do
  ...
end

注意

rake_helper.rbのrequire 'rails_helper'を書いてなかったりrails_helperの設定をしていないと下記のエラーになる。

Failure/Error: Rails.application.load_tasks

NameError:
  uninitialized constant Rails

参考

1
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
1
0