在使用Cucumber时,在OmniAuth中打开测试模式不会重定向请求

时间:2012-01-12 05:58:29

标签: testing cucumber omniauth

我正在尝试通过在向/auth/facebook发出请求时提供伪造的身份验证哈希来测试我的OmniAuth登录过程,如herehere所述。问题是,当我打开测试模式时,请求将作为错误返回,这与未打开测试模式时的行为相同。

user_management.feature

Feature: User management
    @omniauth_test
    Scenario: Login
        Given a user exists
        And that user is signed in

web_steps.rb

...
And /^that user is signed in$/ do
  visit "/auth/facebook"
end
...

omniauth.rb

Before('@omniauth_test') do
  OmniAuth.config.test_mode = true
  p "OmniAuth.config.test_mode is #{OmniAuth.config.test_mode}"
  # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
  OmniAuth.config.mock_auth[:facebook] = {
      "provider"=>"facebook",
      "uid"=>"uid",
      "user_info"=>{"email"=>"test@xxxx.com", "first_name"=>"Test", "last_name"=>"User", "name"=>"Test User"}
  }
end

After('@omniauth_test') do
  OmniAuth.config.test_mode = false
end

结果

Feature: User management

  @omniauth_test
  Scenario: Login                # features/user_management.feature:3
"OmniAuth.config.test_mode is true"
    Given a user exists     # features/step_definitions/pickle_steps.rb:4
    And that user is signed in # features/step_definitions/web_steps.rb:40
      No route matches [GET] "/auth/facebook" (ActionController::RoutingError)
      ./features/step_definitions/web_steps.rb:41:in `/^that user is signed in$/'
      features/testing.feature:5:in `And that user is signed in'

3 个答案:

答案 0 :(得分:0)

问题不在于您的测试。这与您的路由有关,或者更具体地说是与omniauths路由。

您确定在facebook的config / initializers / omniauth.rb中设置了策略吗?

您可以使用gemform https://github.com/mkdynamic/omniauth-facebook

获取它

另外,请记住在添加新策略后重新启动网络服务器。 (这让我有一次;))

答案 1 :(得分:0)

你应该把这两个放在测试初始化​​器中:

request.env["devise.mapping"] = Devise.mappings[:user] 
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]

答案 2 :(得分:0)

在feature / support下的omniauth.rb中抛出以下内容,并使用@omniauth_test标记您需要fb登录的场景

Before('@omniauth_test') do  
  OmniAuth.config.test_mode = true  

  # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
  OmniAuth.config.mock_auth[:facebook] = {
   :provider => 'facebook',
   :uid => '1234567',
   :info => {
     :nickname => 'test',
     :email => 'info@gmail.com',
     :name => 'Test User',
     :first_name => 'Test',
     :last_name => 'User',
     :location => 'California',
     :verified => true
    }.stringify_keys!
  }.stringify_keys!
end

After('@omniauth_test') do
  OmniAuth.config.test_mode = false
end