Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails capybara webkit-rails会话未保留/设置_Ruby On Rails_Session Cookies_Capybara Webkit - Fatal编程技术网

Ruby on rails capybara webkit-rails会话未保留/设置

Ruby on rails capybara webkit-rails会话未保留/设置,ruby-on-rails,session-cookies,capybara-webkit,Ruby On Rails,Session Cookies,Capybara Webkit,我为我的集成测试设置了capybara webkit,我遇到了一个非常简单的问题。未存储我的会话。这个用例非常简单 1. Login 2. Go to a specific page 3. Check if it has the approp content 现在在第2步,我的应用程序将测试用例返回到登录页面-这意味着会话设置不正确 非常感谢您的帮助 如果我使用@culerity而不是@javascript,那么这个测试用例就通过了,所以问题似乎在于水

我为我的集成测试设置了capybara webkit,我遇到了一个非常简单的问题。未存储我的会话。这个用例非常简单

       1. Login
       2. Go to a specific page
       3. Check if it has the approp content
现在在第2步,我的应用程序将测试用例返回到登录页面-这意味着会话设置不正确

非常感谢您的帮助

如果我使用@culerity而不是@javascript,那么这个测试用例就通过了,所以问题似乎在于水豚webkit的设置

我对capybara webkit支持的env.rb如下

    Spork.prefork do
     require 'cucumber/rails'
     require 'capybara'
     require 'capybara/dsl'
     require 'capybara/cucumber'
     require 'capybara-webkit'

     Capybara.run_server = false
     Capybara.javascript_driver = :webkit
     Capybara.default_selector = :css


     # Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
     # order to ease the transition to Capybara we set the default here. If you'd
     # prefer to use XPath just remove this line and adjust any selectors in your
     # steps to use the XPath syntax.
     # Capybara.default_host = "127.0.0.1:3000"

     Capybara.app_host = "http://localhost:3000"
   end
更新1: 看起来正在设置会话。我在步骤中使用以下代码转储会话

     puts(Capybara.current_session.driver.browser.get_cookies)
我得到了以下信息-看起来cookie正在设置中,但没有被返回

["_第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第FBE1C2A77a433228e7b7f2d8c8f0aec3ad5fb5f;HttpOnly;域=本地主机;路径=/“]

更新2: 正在创建错误的树。rails应用程序似乎没有看到我在测试用例中创建的用户,因为我的数据库清理策略设置为事务性。请参阅

为了更加清晰, Capybara webkit或selenium驱动程序运行在另一个线程中,然后是应用程序,因此,如果您使用的是事务装置或数据库清理器,则strategy:transaction和您的数据不会提交到db,其他线程也不会看到它。 可能的解决办法是:

  • 使用带有策略的数据库清理器:截断。(稳定,但速度较慢)
  • 添加代码以强制所有线程使用单个事务进行活动记录。(速度更快,但可能会有一些问题,例如:由于没有提交,因此不会调用after_提交挂钩)

  • 我用的是第二选项,但这是有争议的

    #Capybara use the same connection
    class ActiveRecord::Base
      mattr_accessor :shared_connection
      @@shared_connection = nil 
    
      def self.connection
        @@shared_connection || retrieve_connection
      end 
    end 
    
    # Forces all threads to share the same connection. This works on
    # Capybara because it starts the web server in a thread.
    ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection