1

requestカピバラでは利用できませんが、facebook/twitter からのログインをテストしようとしています。使用できるヘルパーを作成するにはどうすればよいrequestですか?

エラー: NameError: undefined local variable or method 'request'

login_integration_tests.rb:

  before do
    OmniAuth.config.mock_auth[:facebook] = {
      'provider' => 'facebook',
      'uid' => '123545'
    }
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] # error here
  end

ご協力いただきありがとうございます!

4

1 に答える 1

2

私は google_auth を使用してこれを行いましたが、facebook、twitter、および github でも同じことができます。

まず、次の行を削除します。

request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]

これらの行を spec_helper.rb に追加します。

Capybara.default_host = 'http://localhost:3000' #This is very important!

OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:default, {
  :info => {
          :email => 'foobar@test.com',
          :name => 'foo',
          :password => 'qwerty123'
       }
})

次に、ヘルパーで:

module FeatureHelpers
  def login_with_oauth(service = :google_apps)
    visit "/users/auth/#{service}"
  end
end

ヘルパーを spec_helper.rb に含めます

RSpec.configure do |config|

    config.include FeatureHelpers, type: :feature

end

そして最後に、feature/capybara_spec.rb ファイルで

feature "Signing in" do

  before do
    Capybara.current_driver = :selenium
    login_with_oauth #call your helper method
  end

  scenario "Signing in with correct credentials" do
    expect(page).to have_content('Welcome')
  end
end

ちょっと遅れて、答えを見つけたかもしれませんが、できなかった場合、または誰かが読んでいる場合は、これを入手してください。

于 2014-06-18T17:36:35.810 に答える