我有一个场景,当我使用真正的omniauth时效果很好,但是当我在黄瓜/ capybara中使用模拟身份验证运行它时失败了。
在回调中,当我执行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
Auth回调:
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)
答案 0 :(得分:1)
您正在获取用户并将其收集到新变量@user
但是当您再次调用sign_in
方法时,您使用(例如@user
)初始化了新变量用户