LoginSignup
2
2

More than 3 years have passed since last update.

【Capybara】System Specでsessionのテストをする方法

Posted at

環境

RSpec 3.8
capybara (2.16.1)
selenium-webdriver (3.8.0)

設定

spec/support/inject_session.rb
module InjectSession
  def inject_session(hash)
    Warden.on_next_request do |proxy|
      hash.each do |key, value|
        proxy.raw_session[key] = value
      end
    end
  end
end
spec/rails_helper.rb

config.include InjectSession, type: :system 

これで設定は完了。
あとは、先ほど定義したinject_sessionを使って任意の値を入れるイメージです。

spec/system/users_spec.rb
subject { visit user_path }
session = { users: { name: "taro", age: 20 } }

context "sessionがある場合" do
  let(:session) { { users: { name: "taro", age: 20 } } }
  before do
    inject_session session
    subject
  end
  .....
end

context "sessionがない場合" do
  let(:session) {}
  before do
    inject_session session
    subject
  end
  ......
end

参考: https://stackoverflow.com/questions/8546839/how-do-i-set-up-a-session-value-in-capybara

2
2
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
2
2