简体   繁体   English

设计控制器测试-ActionController :: UrlGenerationError

[英]Devise Controller Test - ActionController::UrlGenerationError

I'm using OmniAuth through Devise in my Rails app. 我在Rails应用程序中通过Devise使用OmniAuth。 I'm trying to test that my callback method is being called properly and functions correctly. 我正在尝试测试我的回调方法是否被正确调用并且功能正常。 I am currently receiving an error when running my spec. 运行规范时,我目前收到错误消息。

The error: 错误:

Failure/Error: get user_omniauth_authorize_path(:facebook)
ActionController::UrlGenerationError:
    No route matches {:action=>"/users/auth/facebook", :controller=>"users/omniauth_callbacks"} missing required keys: [:action]

My spec: 我的规格:

#spec/controllers/users/omniauth_callbacks_controller_spec.rb
require 'rails_helper'

RSpec.describe Users::OmniauthCallbacksController, :type => :controller do
  context 'get facebook' do
    before do
      request.env["devise.mapping"] = Devise.mappings[:user] # If using Devise
      request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
    end
    it 'should create user, redirect to homepage, and create session' do
      get user_omniauth_authorize_path(:facebook)
      expect(response).to redirect_to(user_omniauth_callback_path)
    end
  end
end

Support file: 支持文件:

#spec/support/omniauth.rb
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
                                                              :provider => 'facebook',
                                                              :uid => '123545',
                                                              :email => 'fake@fake.com'
                                                          })

Controller: 控制器:

#app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.from_omniauth(request.env['omniauth.auth'])

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => 'Facebook') if is_navigational_format? #todo what is this doing
    else
      session['devise.facebook_data'] = request.env['omniauth.auth']
      redirect_to new_user_registration_url
    end
  end
end

Routes: 路线:

devise_for :users, :controllers => { :omniauth_callbacks => 'users/omniauth_callbacks' }

I think the issue is in how it's getting routed. 我认为问题在于如何进行路由。 I think action should just be 'facebook' not '/users/auth/facebook', but I don't know the right way to resolve this. 我认为动作应该只是“ facebook”而不是“ / users / auth / facebook”,但是我不知道解决此问题的正确方法。

In case anyone stumbles on this looking for answers like I did. 如果有人像我一样偶然发现这个问题寻找答案。 I had this problem when adding a second Omniauth strategy. 添加第二个Omniauth策略时遇到了这个问题。 Turns out the problem was I'd forgotton to include the strategy in my model declaration 原来问题是我忘了将该策略包含在模型声明中

eg I was already authorising with google 例如我已经在用谷歌授权

# app/models/user.rb
devise :rememberable, :trackable, :omniauthable, :omniauth_providers => [:google]

but then wanted to add a second provider (eg facebook). 但随后想添加第二个提供者(例如,facebook)。 I forgot to add facebook to the list of omniauth providers and so was getting that error when my spec ran. 我忘了将facebook添加到omniauth提供程序的列表中,因此在我运行规范时遇到了该错误。 I fixed it by changing to 我通过更改为

# app/models/user.rb
devise :rememberable, :trackable, :omniauthable, :omniauth_providers => [:google,:facebook]

I got the same error missing required keys: [:action] as you. 我也遇到相同的错误, missing required keys: [:action]和您一样。 After I read RSpec document, I found that the argument of get should be something like :index (the action name). 阅读RSpec文档后,我发现get的参数应该类似于:index (动作名称)。 Because I defined: 因为我定义了:

# app/controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # ...
  end
end

So I changed get user_omniauth_authorize_path(:facebook) to get :facebook and added some omniauth mocks. 所以我将get user_omniauth_authorize_path(:facebook)更改为get :facebook并添加了一些omniauth模拟。 Now it pass! 现在过去了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActionController :: UrlGenerationError:尝试测试控制器时没有路由匹配 - ActionController::UrlGenerationError: No route matches when trying to test the controller 注释conrtoller测试ActionController :: UrlGenerationError - Comments conrtoller test ActionController::UrlGenerationError Devise :: Registrations#create中的ActionController :: UrlGenerationError - ActionController::UrlGenerationError in Devise::Registrations#create 尝试加载form_tag和控制器动作时,设计ActionController :: UrlGenerationError - Devise ActionController::UrlGenerationError when trying to load form_tag and controller action 测试自定义设计控制器时的ActionController :: RoutingError - ActionController::RoutingError when test custom devise controller Rails控制器测试:ActionController :: UrlGenerationError:没有路由匹配 - Rails Controller Testing: ActionController::UrlGenerationError: No route matches ActionController :: UrlGenerationError:没有路由匹配动作和控制器 - ActionController::UrlGenerationError: No route matches action and controller RSpec 错误 ActionController::UrlGenerationError: 测试控制器时 - RSpec error ActionController::UrlGenerationError: when testing controller 集成测试在Rails 4中获取ActionController :: UrlGenerationError - Integration test get ActionController::UrlGenerationError in rails 4 ActionController :: UrlGenerationError:没有路线与{:action =&gt;“ edit”,:controller =&gt;“ posts”}匹配-抽水测试失败,但应用正常运行 - ActionController::UrlGenerationError: No route matches {:action=>“edit”, :controller=>“posts”} - rake test fails but app works correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM