为什么在一次活动后水豚会放弃我的会话? [英] Why is Capybara discarding my session after one event?

查看:85
本文介绍了为什么在一次活动后水豚会放弃我的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试大多数控制器具有before_filter :authenticate_user!的Rails应用程序,我无法让Capybara保留会话.

我让Capybara与poltergeist一起使用了PhantomJS.

我使用以下助手:

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',则should声明将失败,因为我将被重定向到应用程序的登录页面. /p>

我尝试了几种不同的方法,包括设置 Capybara :: Session ,但我仍然可以在AJAX请求中获得401代码,并且每个规范只能成功访问一次.

我在做什么错了?

解决方案

所以问题是phantomjs驱动程序(poltergeist)使用的是单独的数据库连接.之前我遇到了完全相同的问题,我从 railscast第391页找到了一个解决方案,其中的代码为spec/support/shared_db_connection.rb

由于找不到他的代码的许可证,因此我将链接到

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.

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.

What am I doing wrong?

解决方案

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

这篇关于为什么在一次活动后水豚会放弃我的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆