callbackredirect不保存会话

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

在callback中,当我做sign_in @user ,它成功创build用户并login…设置了current_user 。 但是,当我然后做redirect_to request.env['omniauth.origin'] || '/' redirect_to request.env['omniauth.origin'] || '/' ,在current_user的动作中, current_user现在是零。

我已经通过截图/暂停浏览器证实,它不与模拟身份validation工作。 在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 

Authcallback:

 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) 

您正在获取用户并将其收集到新的variables@user但是当您再次调用sign_in方法时,您已经使用(例如@user )初始化了新的variables用户,