2012-01-12 57 views
1

,當我試圖當一個請求到/auth/facebook製成,如描述herehere提供僞造的認證哈希來測試我的OmniAuth登錄過程不將請求重定向。問題是,當我打開測試模式時,請求返回爲錯誤,這與未打開測試模式時的行爲相同。打開在OmniAuth測試模式下使用黃瓜

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"=>"[email protected]", "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' 

回答

0

問題不在於您的測試。它與您的路由,或者更具體地說與omniauths路由。

你確定你有一個在config/initializers/omniauth.rb中爲facebook設置的策略嗎?

可以在gemform https://github.com/mkdynamic/omniauth-facebook

得到它此外,請記住添加新策略後重新啓動你的web服務器。 (這讓我一次;))

0

你應該在測試初始化​​拋出這兩個:

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

扔在功能/支持omniauth.rb以下,並且你的情況需要FB登錄與@ omn​​iauth_test

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 => '[email protected]', 
    :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