環境
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
参考