CableReady helps you create great real-time user experiences by making it simple to trigger client-side DOM changes from server-side Ruby.
This gem makes the testing of your broadcast classes easier by providing custom matchers that will verify that the expected message was broadcasted to the expected channel.
Open Gemfile and add the following line to the test group:
group :test do
gem 'cable-ready-testing'
endnow load the library for RSpec by editing the file spec/rails_helper.rb and loading the gem after initializing the environment with the following line:
require 'cable_ready/testing/rspec'you are now ready to use the matchers inside your RSpec tests.
Let's consider the following usage of Cable Ready:
class Broadcaster
include CableReady::Broadcaster
def call(channel_name, selector)
cable_ready[channel_name].outer_html(
selector: selector,
html: 'html'
)
cable_ready.broadcast
end
endwithout custom matchers you may end-up with the following test:
RSpec.describe Broadcaster do
subject { described_class.new }
describe '#call' do
it 'broadcasts the html' do
cable_ready = double(outer_html: double)
expect(CableReady::Channels.instance)
.to receive(:[])
.with('custom_channel')
.and_return(cable_ready)
expect(cable_ready)
.to receive(:outer_html)
.with(selector: '#some-div', html: 'html')
expect(CableReady::Channels.instance)
.to receive(:broadcast).once
subject.call('custom_channel', '#some-div')
end
end
endafter using cable-ready-testing gem:
RSpec.describe Broadcaster do
subject { described_class.new }
describe '#call' do
it 'broadcasts the html' do
expect {
subject.call('custom_channel', '#some-div')
}.to mutated_element('#some-div')
.on_channel('custom_channel')
.with(:outer_html, { 'html' => 'html' })
end
end
endThe following matchers are available:
mudated_elementmutated_attributemutated_css_classmutated_datasetmutated_stylemutated_element
Mentioned above matchers work the same way, you should choose the right one depending on the context. If you are calling cable_ready["MyChannel"].set_dataset_property then use mutated_dataset matcher, etc. Always chain them with .on_channel and .with.
CableReady testing lib is released under the MIT License.