可以使用Capybara访问Rspec中的会话变量

时间:2012-08-28 21:46:19

标签: ruby-on-rails rspec capybara

是否可以使用Capybara和Selenium驱动器访问Rspec中的会话变量?

我将如何做这样的事情(我正在使用omniauth-facebook)?

require 'spec_helper'

describe 'Api' do
  def login_with_oauth
    visit "/auth/facebook"
  end

  it 'get location count for specific user' do
    login_with_oauth
    @user=User.find(session[:user_id])
    #puts "here is response body: #{response.body}"

我明白了 How to inspect the session hash with Capybara and RSpec?但是没有得到我真正想要的东西。

THX

1 个答案:

答案 0 :(得分:1)

我想要考虑一下你实际上不需要会话。如果我错了,我很抱歉。

那是how you can test omniauth。

# spec/spec_helper.rb
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new
OmniAuth.config.add_mock(:facebook, {
  provider: "facebook",
  uid: "123456",
  info: {
    email: "bruce.wayne@batman.com",
    first_name: "Bruce",
    last_name: "Wayne"
  }
})


# spec/request/api_spec.rb
let!(:user) { create :user, email: "bruce.wayne@batman.com" }

before do
  visit "/auth/facebook"
end

it 'get location count for specific user' do
  user.id # contains your user id
end