为什么 Capybara 在一次活动后放弃我的会话? [英] Why is Capybara discarding my session after one event?
问题描述
对于大多数控制器测试具有 before_filter :authenticate_user!
的 rails 应用程序,我无法让 Capybara 保留会话.
Testing a rails application which has before_filter :authenticate_user!
for most controllers, I cannot get Capybara to preserve a session.
我使用 PhantomJS 和 poltergeist 配置了 Capybara.
I have Capybara configured using PhantomJS with poltergeist.
我使用以下助手:
require 'spec_helper'
include Warden::Test::Helpers
module FeatureHelpers
def login(user = FactoryGirl.create(:default_user))
login_as user, scope: :user
user
end
end
我有以下规范文件:
require 'spec_helper'
include Warden::Test::Helpers
feature 'Leads Data Tasks View' do
before(:each) do
@user = login
end
after{ Warden.test_reset! }
context "clicking a task button" do
scenario "login persists across multuple actions", js: true do
visit '/tasks'
page.should have_selector('#parse', count: 1)
end
end
end
当我按照此处显示的方式运行测试时,它会通过.但是,如果我在执行 AJAX 操作的某些内容上调用 click_link
,或者如果我只是尝试执行 visit '/tasks'
两次,应该
断言将失败,因为我将被重定向到应用程序的登录页面.
When I run the test as it's shown here, it will pass. However, if I invoke a click_link
on something that performs AJAX actions, or if I simply try to do visit '/tasks'
twice, the should
assertion will fail because I'll get redirected to the login page of the app.
我尝试了几种不同的方法,包括设置 Capybara::Session,但我仍然收到 AJAX 请求的 401 代码,并且每个规范只能成功访问一次.
I've tried a few different approaches, including setting up a Capybara::Session, but I still get 401 codes on AJAX requests and I can only successfully visit once per spec.
我做错了什么?
推荐答案
所以问题是 phantomjs 驱动程序 (poltergeist
) 使用的是分离的数据库连接.我之前遇到过完全相同的问题,我从 railscast 第 391 集 中得到了解决方案,代码为spec/support/shared_db_connection.rb
So the problem was that phantomjs driver (poltergeist
) was using a separated database connection. I had the exact same issue before and I got a solution from railscast episode 391 with the code in spec/support/shared_db_connection.rb
因为我找不到他的代码的许可证,所以我只链接到 代码在这里
Since I can't find license for his code so I will just link to the code here
这篇关于为什么 Capybara 在一次活动后放弃我的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!