rails3.2 + rspec + capybara1.0测试设计google-oauth2.0

时间:2013-12-04 23:12:39

标签: ruby-on-rails devise oauth-2.0 capybara

我正在使用带有ruby 1.9.2 rails 3.2的capybara 1.0 这是我的考验:

#spec/features/google_login_spec.rb
require 'spec_helper'
describe "the signin process", type: :feature do
  before do
    puts user_omniauth_authorize_path(:google_oauth2) #/users/auth/google_oauth2
    visit  user_omniauth_authorize_path(:google_oauth2)
  end
  it {#somethings}
end

但我有这个错误:

  Failures:

  1) the signin process 
     Failure/Error: visit  user_omniauth_authorize_path(:google_oauth2)
     ActionController::RoutingError:
       No route matches [GET] "/o/oauth2/auth"
     # ./spec/features/google_login_spec.rb:9:in `block (2 levels) in <top (required)>'

我的路线文件是这个

#config/routes.rb
Booking::Application.routes.draw do
  #https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview#using-omniauth-without-other-authentications
  devise_for :users , controllers: {omniauth_callbacks: "users/omniauth_callbacks"}
  devise_scope :user do
    get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
    get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
  end
end

$rake routes
user_omniauth_authorize GET|POST /users/auth/:provider(.:format)        users/omniauth_callbacks#passthru {:provider=>/google_oauth2/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:google_oauth2)
new_user_session GET      /sign_in(.:format)                     devise/sessions#new
destroy_user_session GET      /sign_out(.:format)                    devise/sessions#destroy

应用程序运行正常,没有问题。问题是当水豚访问user_omniauth_authorize_path(:google_oauth2)时。 我不知道要解决,你能帮助我吗?

解决方案:

#spec/features/google_login_spec.rb
require 'spec_helper'
describe "the signin process", type: :feature do
  before do
    puts user_omniauth_authorize_path(:google_oauth2) #/users/auth/google_oauth2
    Capybara.current_driver = :selenium #change drive according whit https://github.com/jnicklas/capybara#drivers 
    visit  user_omniauth_authorize_path(:google_oauth2)
  end
  it {#somethings}
end

1 个答案:

答案 0 :(得分:3)

Capybara使用的默认RackTest驱动程序不支持重定向到外部站点。您需要使用Selenium或其他替代驱动程序。

有关选择驱动程序的说明,请参阅https://github.com/jnicklas/capybara#drivers