How can I configure puma when running Capybara?

被刻印的时光 ゝ 提交于 2020-01-01 08:45:12

问题


I'd like to adjust the puma configuration when running Capybara tests. Changing settings in .env, .env.test (I use dotenv), or config/puma.rb has no effect.

Where can I change the configuration?

Rails 5.1, poltergeist 1.15.0, capybara 2.14.0, puma 2.8.2


回答1:


Generally with Capybara you configure the server in a register_server block. The :puma server definition Capybara provides is

Capybara.register_server :puma do |app, port, host|
  require 'rack/handler/puma'
  Rack::Handler::Puma.run(app, Host: host, Port: port, Threads: "0:4")
end

If you're using Rails 5.1 system testing it has added a layer of abstraction on top of that with server configuration being done in ActionDispatch::SystemTesting::Server#register which is defined as

def register
  Capybara.register_server :rails_puma do |app, port, host|
    Rack::Handler::Puma.run(app, Port: port, Threads: "0:1")
  end
end

Either way you should be able to either overwrite one of the current server registrations

Capybara.register_server :rails_puma do |app, port,host|
  Rack::Handler::Puma.run(app, ...custom settings...)
end

or configure your own

Capybara.register_server :my_custom_puma do |app, port, host|
  Rack::Handler::Puma.run(app, ...custom settings...)
end

Capybara.server = :my_custom_puma



回答2:


In ApplicationSystemTestCase, you can configure by passing options to the default :puma server used by Rails in setup.

AFAIK, this will work for any options, but I've only used

  • Silent: true to silence the puma startup output
  • Thread: '1:1' to configure the puma process to only use one thread

Here's how I've setup up rails systems tests to run inside docker containers:

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: { url: "http://selenium:4444/wd/hub" }

  def setup
    Capybara.server_host = '0.0.0.0' # bind to all interfaces
    Capybara.server = :puma, { Silent: true, Threads: '1:1' }
    host! "http://#{IPSocket.getaddress(Socket.gethostname)}"
    super
  end
end


来源:https://stackoverflow.com/questions/43926898/how-can-i-configure-puma-when-running-capybara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!