我在rails 3.2.11下使用design2.2.0和omniauth 1.1.1。
我有一个测试在一个未认证(例如没有"before_filter:authenticate_user!")控制器名为"HomepageController"与一个名为"主页"的动作。这个控制器动作非常简单,除非用户登录,否则它会传递到它的视图,在这种情况下,它会重定向到我们的"主页"页面。下面是控制器动作:
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
这里是我的设计和全功能路由:
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上时都能完美地工作。只有在测试中失败。
非常感谢任何见解!
我今天遇到了同样的问题,发现我没有在测试环境中添加Facebook作为提供者。这应该在devise.rb
初始化器中完成。我猜这就是运行测试时没有生成路由的原因。
#config/initializers/devise.rb
if Rails.env.test?
config.omniauth :facebook, "APP_ID", "APP_SECRET"
end