forked from websocket-rails/websocket-rails
-
Notifications
You must be signed in to change notification settings - Fork 17
Testing WebsocketRails Controllers
nessche edited this page Feb 6, 2013
·
2 revisions
The websocket-rails
ships with a few custom matchers to help testing your WebsocketRails Controller and your Event Router.
The routing matchers, as the name suggest, verify that the events and controllers defined in the events.rb
file are behaving as expected.
-
be_routed_to
verifies that a given event is non-exclusively routed to the desired controller -
be_routed_only_to
on the other hand verifies that a given event is routed to and only to the desired controller.
The routing matchers accept as parameter the expected target, either expressed as a Hash ({ :to => ProductController, :with_method => :update_product}} or as a String ('product#update_product').
In other words if your controllers look like
# app/controllers/websocket_controllers.rb
class ProductController < WebsocketRails::BaseController
def update_product
# code here
end
def delete_product
# code here
end
end
class WarehouseController < WebsocketRails::BaseController
def remove_product
# code here
end
end
and your events.rb
is
# app/config/initializers/events.rb
WebsocketRails::EventMap.describe do
namespace :product do
subscribe :update, :to => ProductController, :with_method => :update_product
subscribe :delete, :to => ProductController, :with_method => :delete_product
subscribe :delete, :to => WarehouseController, :with_method => :remove_product
end
end
then you could write the following spec file
# spec/config/initializers/events_spec.rb
describe 'Event Mapping for MyApp' do
describe 'product.update' do
it 'should be routed correctly' do
create_event('product.update', nil).should be_only_routed_to 'product#update_product'
end
end
describe 'product.delete' do
it 'should be routed correctly' do
# feel free to split the should statements into separate examples, not done here for brevity's sake
create_event('product.delete', nil).should be_routed_to 'product#delete_product'
create_event('product.delete', nil).should be_routed_to 'warehouse#delete_product'
end
end
end
Notice the usage of create_event(message_name, data)
to instantiate the event.