使用rails cucumber测试facebook登录

时间:2014-04-09 05:13:54

标签: ruby-on-rails facebook cucumber omniauth facebook-login

我在this tutorial之后在我的网站上设置了Facebook登录,并使用黄瓜和水豚。我尝试过关注如何设置虚假登录帐户的其他SO帖like this。如果我直接使用它,我会得到:

When I follow "sign_in"                                        # features/step_definitions/web_steps.rb:56
      No route matches [GET] "/oauth/authorize" (ActionController::RoutingError)
      ./features/step_definitions/web_steps.rb:57:in `/^(?:|I )follow "([^"]*)"$/'
      features/facebook_signin.feature:9:in `When I follow "sign_in"'

如果我在路线中添加get "/oauth/authorize",我会:

When I follow "sign_in"                                        # features/step_definitions/web_steps.rb:56
      uninitialized constant OauthController (ActionController::RoutingError)
      ./features/step_definitions/web_steps.rb:57:in `/^(?:|I )follow "([^"]*)"$/'
      features/facebook_signin.feature:9:in `When I follow "sign_in"'

我不知道发生了什么以及为什么抱怨。如果我将我的Gemfile从gem 'omniauth-facebook', '1.4.0'更改为gem 'omniauth-facebook',我会得到几乎相同的错误,除非代替:

/oauth/authorize,我得/dialog/oauth而不是uninitialized constant OauthController,我得到uninitialized constant DialogController

最近有没有人成功设置黄瓜测试登录Facebook?

当我在localhost:3000并导航到localhost:3000 / auth / facebook时,一切正常,我正在使用sessionsController,所以我不明白为什么在测试中,它试图使用这些oauthControllers或DialogueControllers。

3 个答案:

答案 0 :(得分:5)

我最近有同样的问题或非常类似的问题:

ActionController::RoutingError:
  No route matches [GET] "/dialog/oauth"

我已经采用了正确设置来自另一个项目的模拟响应的工作规范,并且很惊讶突然发现此错误。

在经历了多次痛苦和痛苦之后,当我意识到我忘记将OmniAuth设置为使用测试模式时,我有一个重要的面孔时刻:

# spec/rails_helper.rb
OmniAuth.config.test_mode = true

这将导致OmniAuth短路,以便您可以通过以下方式设置auth响应:

OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
  :provider => 'facebook',
  :uid => '123545'
  # etc.
})

我认为错误是由于OmniAuth在测试环境中使用:developer策略试图提供帮助,因此您不会被身份验证提供程序禁止。

请参阅https://github.com/intridea/omniauth/wiki/Integration-Testing

答案 1 :(得分:1)

您是否在此测试中启用了Javascript?正如教程中提到的facebook.js.coffee?

describe 'When I follow "sign in"', js: true do 

另一种可能性是您只将gem包含在Gemfile的development块中

group :development do
  gem 'omniauth-facebook'
end
顺便说一句:你不应该对#34;真实"进行测试。外部端点。看看webmock进行此类测试以模拟Facebook响应

答案 2 :(得分:0)

我必须在功能之前放置@omniauth_test_success而不是步骤定义。另外,我不得不修复一些路由问题。将在本周晚些时候发布完整报告。