Skip to content

Instantly share code, notes, and snippets.

@daniel-nelson
Created June 30, 2015 18:31
Show Gist options
  • Save daniel-nelson/18465dd73a2e8f5baff0 to your computer and use it in GitHub Desktop.
Save daniel-nelson/18465dd73a2e8f5baff0 to your computer and use it in GitHub Desktop.
After adding pubsub (using Pubnub.com) to our application, our headless specs were straight up breaking. Solved by creating a mock that we can trigger within the specs
Coffeescript required by application.js manifest:
class PubsubMock
subscribe: (obj) ->
@channels ||= {}
@channels[obj.channel] ||= []
@channels[obj.channel].push(obj.message)
publish: (channel, message) ->
for callback in @channels[channel]
callback(message)
window.pubsub_listener ||= new PubsubMock()
spec/support/pubsub_mock.rb
module PubsubMock
def pubsub_publish(channel, message)
script = "window.pubsub_listener.publish('#{channel}', '#{message.to_json}')"
page.execute_script(script)
end
end
In a feature spec:
pubsub_publish('some channel', pubsub_address: user.pubsub_address, event: 'some event')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment