46

我有一个场景在我使用真正的omniauth 时工作得很好,但是当我在黄瓜/水豚中使用模拟身份验证运行它时失败了。

在回调中,当我这样做时sign_in @user,它会成功创建用户并登录...current_user已设置。但是当我这样做时redirect_to request.env['omniauth.origin'] || '/',在接下来的动作中,current_user现在为零。

我已经通过屏幕截图/暂停浏览器确认它不适用于模拟身份验证。Firefox 和 chrome 驱动程序中也会出现同样的错误。

知道为什么会发生这种情况吗?

/features/support/env.rb:

Cucumber::Rails::Database.javascript_strategy = :truncation

设想:

@javascript
Scenario:
    Given I am on the home page
    When I press "Login"
    And I should see "Login with Twitter" in the selector "#login-modal"
    Given Omniauth returns a user with provider "twitter" and uid "1" and nickname "foo"    
    When I login with Twitter
    Then I should be logged in as "foo"

步骤定义:

Given(/^Omniauth returns a user with provider "(.*?)" and uid "(.*?)" and nickname "(.*?)"$/) do |provider, uid, nickname|
  OmniAuth.config.test_mode = true

 OmniAuth.config.add_mock(provider.to_sym, {
     :uid => uid,
     :info => {
       :name => nickname
     }
   })
end

Then(/^I should be logged in as "(.*?)"$/) do |nickname|
  expect(page).to have_content(nickname)
end

授权回调:

def twitter
  @user = User.from_omniauth(request.env["omniauth.auth"]) # this works-- I get the mock
  sign_in @user
  puts ">> in auth callback: just signed in user #{current_user.id}"        
  redirect_to request.env['omniauth.origin'] || '/'
end

控制器:

def new
  puts ">> in my_controller#new: current_user = #{current_user.id if current_user}"
end

黄瓜产量:

Given Omniauth returns a user with provider "twitter" and uid "1" and nickname "foo" 
>> in auth callback: just signed in user 1
>> in my_controller#new: current_user =
When I login with Twitter                                                                 
Then I should be logged in as "foo"                                                  
  expected to find text "foo" in [redacted] (RSpec::Expectations::ExpectationNotMetError)
4

1 回答 1

1

您正在获取用户并将其收集到新变量@user中,但是当您再次调用该方法时,您确实使用 using(eg. )sign_in初始化了新变量 user@user

于 2016-10-03T09:31:49.313 回答