Skip to content

Instantly share code, notes, and snippets.

@alex-raz
Last active December 1, 2018 09:35
Show Gist options
  • Save alex-raz/98448c9935352fac5ee96e139bc3bd72 to your computer and use it in GitHub Desktop.
Save alex-raz/98448c9935352fac5ee96e139bc3bd72 to your computer and use it in GitHub Desktop.
ActionCable testing with Capybara
# 3.7.0 has some issues with `port` option
gem 'puma', '>= 3.7.1'
group :test do
# starting from 2.7.0 Capybara allows to specify the named server to use
gem 'capybara', '>= 2.7.0'
# just for reference
gem 'selenium-webdriver'
end
# spec/features/action_cable_feature_spec.rb
require 'capybara_helper'
RSpec.feature 'Action cable integration spec', type: :feature do
it 'changed value appears without page refreshing', js: true do
value = 'New Value'
visit root_path
expect(page).not_to have_content(value) # sanity check
# submit form in new window
new_window = window_opened_by { click_link 'Page with form' }
within_window new_window do
fill_in 'Value', with: value
click_on 'Save'
end
# check for new value in previous window without page refreshing
switch_to_window(windows.first)
expect(page).to have_text(value)
end
end
# config/cable.yml
# You might also need your ActionCable features to work in rails console:
#
# >> ActionCable.server.broadcast 'example_channel', message: '<p>Test</p>'
# [ActionCable] Broadcasting to example_channel: {:message=>"<p>Test</p>"}
# => []
#
# The default behavior for ActionCable in development mode is to use the async adapter,
# which operates within the same process only. For inter-process broadcasting, you will need to switch to the redis adapter.
redis: &default
adapter: redis
url: redis://localhost:6379/1
production: *default
development: *default
test: *default
# spec/capybara_helper.rb
require 'rails_helper'
require 'capybara/rspec'
# specify js driver
Capybara.javascript_driver = :selenium
# for testing ActionCable with Capybara we need a multithreaded webserver
Capybara.server = :puma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment