【RSpec】モジュールの使用方法について
はじめに
Rspecモジュールの作成・使用方法について紹介します。
初期設定に加えて
rails_helper
# This file is copied to spec/ when you run 'rails generate rspec:install' require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } #コメントアウトをはずしましょう RSpec.configure do |config| config.include LoginSupport #ここでsupport配下のものを呼び込んでいます。 end
これで、spec/support/
配下のものが呼び出せます。
例えば、Loginモジュールを作成できたりします。
login_support.rb(ここの記載はcapybaraを使用しています。)
module LoginSupport def login(user) visit root_path fill_in 'user_name', with: user.name click_button '次へ' fill_in 'user_password', with: 'password' click_button 'ログイン' end end
これで、spec内でモジュールのメソッドが使用でき流ようになります。