【发布时间】:2015-04-12 04:58:00
【问题描述】:
当我使用真正的omniauth 时,我有一个场景可以正常工作,但是当我在cucumber/capybara 中使用mock auth 运行它时失败了。
在回调中,当我执行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)
【问题讨论】:
-
这个问题有什么解决办法吗?你有什么发现吗?
-
好吧,请检查请求的标题是否真实和模拟,也许你应该在模拟中添加一些东西
标签: cucumber capybara omniauth omniauth-twitter