Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara not working with action_cable

I'm using the Rails 5 beta 3 with action cable, the integration works fine in development but when I try to run a feature test through capybara, it doesn't seem to hit the channel actions.

I'm using Portergeist and configured puma as capybara's server. Also I'm using es5-shim and es6-shim.

Has anyone else experienced this or knows any workaround?

Thanks!

Edit

Im using this capybara branch to configure Puma in Capybara

Capybara.register_server :puma do |app, port, host|
  require 'puma'
  Puma::Server.new(app).tap do |s|
    s.add_tcp_listener host, port
  end.run.join
end

I have not set anything on config.action_cable.allowed_request_origins

like image 845
Sergio D. Márquez Avatar asked Mar 09 '16 16:03

Sergio D. Márquez


People also ask

How does action cable work with Ruby?

The Ruby side of things is built on top of websocket-driver , nio4r, and concurrent-ruby. Action Cable is powered by a combination of WebSockets and threads. Both the framework plumbing and user-specified channel work are handled internally by utilizing Ruby's native thread support.

How to connect to the cable server using actioncable?

You can use ActionCable.createConsumer () to connect to the cable server if action_cable_meta_tag is invoked in the layout. Otherwise, A path is specified as first argument to createConsumer (e.g. ActionCable.createConsumer ("/websocket") ).

Which database adapter should I use with actioncable?

Like a database, you can tell ActionCable what type of adapter to use. The adapter choices are async (inline, used for development or testing), Redis (used for production) or PostgreSQL (does not seem very prevalent in the ActionCable use case).

What is a consumer in action cable?

The client of a WebSocket connection is called the consumer. In Action Cable, the consumer is created by the client-side JavaScript framework. Each consumer can, in turn, subscribe to multiple channels. Each channel encapsulates a logical unit of work, similar to what a controller does in a typical MVC setup.


2 Answers

For testing actioncable with Capybara you need to be using a multithreaded webserver. Since you're using a current pull request on Capybara that supports registering named drivers you will need to specify the named server to use

Capybara.server = :puma

For anyone not using the capybara branch with named servers you can do

Capybara.server {|app, port| 
  require 'puma'
  Puma::Server.new(app).tap do |s|
    s.add_tcp_listener Capybara.server_host, port
  end.run.join
}
like image 66
Thomas Walpole Avatar answered Oct 02 '22 13:10

Thomas Walpole


From Capybara v2.7.0 passing a block to Capybara::server is deprecated (commit).

Deprecation message: DEPRECATED: Passing a block to Capybara::server is deprecated, please use Capybara::register_server instead

To register new web server (for example puma) use:

  Capybara.register_server :puma do |app, port, host|
    require 'puma'
    Puma::Server.new(app).tap do |s|
      s.add_tcp_listener host, port
    end.run.join
  end

Link to documentation

like image 22
NickGnd Avatar answered Oct 04 '22 13:10

NickGnd