我在rails 3.2.11下使用Devise 2.2.0和omniauth 1.1.1。
我对一个名为“HomepageController”的未经过身份验证(例如没有“before_filter:authenticate_user!”)控制器进行了测试,该控制器的操作名为“homepage”。此控制器操作非常简单,因为除非用户已登录,否则它将传递到其视图,在这种情况下,它会重定向到我们的“homebase”页面。以下是控制器操作:
class HomepageController < ApplicationController
def homepage
if current_user then
redirect_to :homebase
end
end
end
这是我的测试:
require 'test_helper'
class HomepageControllerTest < ActionController::TestCase
def setup
@user = users(:donley)
end
test "should get homepage when signed in" do
sign_in @user
get :homepage
assert_redirected_to "/homebase"
end
test "should get homepage when not signed in" do
get :homepage
assert_response :success
end
end
第二次测试因此错误而失败:
No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:facebook}
在视图中的这一行:
= link_to user_omniauth_authorize_path(:facebook) do
这是我的设计和omniauth路线:
resources :oauth_clients
match '/oauth/test_request', :to => 'oauth#test_request', :as => :test_request
match '/oauth/token', :to => 'oauth#token', :as => :token
match '/oauth/access_token', :to => 'oauth#access_token', :as => :access_token
match '/oauth/request_token', :to => 'oauth#request_token', :as => :request_token
match '/oauth/authorize', :to => 'oauth#authorize', :as => :authorize
match '/oauth', :to => 'oauth#index', :as => :oauth
devise_for :users, :controllers => {:omniauth_callbacks => "omniauth_callbacks"}
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
get "sign_up", :to => "devise/registrations#new"
match "sign_out", :to => "devise/sessions#destroy"
end
以下是来自rake routes
的路线,类似于错误消息中提到的路线:
user_omniauth_authorize /users/auth/:provider(.:format) omniauth_callbacks#passthru {:provider=>/facebook|linkedin/}
真正疯狂的是,在我的生产环境中rails server
或Heroku上运行时,所有这些代码都能正常运行。它只是在测试中失败。
非常感谢任何见解!
答案 0 :(得分:0)
我今天遇到了同样的问题,发现我没有在测试环境中添加Facebook作为提供商。这应该在devise.rb
初始值设定项中完成。我想这就是我运行测试时没有生成路由的原因。
#config/initializers/devise.rb
if Rails.env.test?
config.omniauth :facebook, "APP_ID", "APP_SECRET"
end