Testing Rails apps that use Pusher with pusher-fake

· Last update: 28 Jun 2013 · Semaphore News

Pusher is a great service for sending messages from anywhere to your users’ web browsers. It has all the good features of a cloud service: reliable uptime, great customer service and it enables you to outsource a complicated but probably not critical part of infrastructure.

Within Semaphore we’re using Pusher to send various status updates. For example, our build page has many moving parts but they’re clustered in a few page states. When a build starts, a background job sends a signal to all connected browsers to silently reload the page using Turbolinks and initialize new behaviour.

With an external API doing an important job, testing your system can be tricky. The common solution is to fake the related component with stubbed methods. For example, to test our GitHub integration, we use VCR to store and play back real responses. For Pusher we’d like a different kind of faking: ideally we need to keep the signal going through our backend to the browser but over localhost.

pusher-fake is a fake Pusher server that lets you have just that. The only change to your code is to conditionally load its’ JavaScript in test environment:

And replace the Pusher library’s implementation in the Rails app. If you’re using Cucumber:

require "pusher-fake/cucumber"

If not, recreate cucumber.rb in your initializer. Now you’re ready to run scenarios which trigger code that sends and receives messages.