Tristan Dunn

Integration Testing Pusher in Ruby

Real-time websites and notifications have become more and more prominent over the past few years. And unless you’re looking to spend a decent amount of time supporting different browsers you’ll be using a service like Pusher. It has been around since 2010 and is used by websites like Travis CI and UserVoice.

If you’re a Ruby developer you’re probably going to have a strong desire to test Pusher integration. Depending on your usage you might be able to get away with some JavaScript and request stubbing, but it can quickly become a hassle on larger projects. That’s where pusher-fake comes in, which is already used to test production applications.

Getting Started

I’ve tried to make the fake simple to use while attempting to avoid modifying your production code as little as possible.

Dependency

First you’ll need to require the dependency in the test environment.

group :test do
  gem "pusher-fake"
end
Adding the library as a test dependency in the Gemfile.

JavaScript

Next you’ll need to use some custom JavaScript for creating a Pusher instance, which sets the appropriate options, such as the API key, host, and port.

<% if defined?(PusherFake) %>
  var instance = <%= PusherFake.javascript %>;
<% else %>
  var instance = new Pusher(...);
<% end %>
Connecting with library generated JavaScript when testing.

Server

And finally if you’re using Cucumber you can easily start up the properly configured servers by including a helper file.

require "pusher-fake/support/cucumber"
Initializing the fake server for Cucumber.

If you’re not using Cucumber, see the helper file on how to do it manually.

Usage

Your application should just work as is for testing now, even when offline. Which includes triggering events from the server, triggering and responding to webhooks, and triggering and responding to events on the client.

If you want to see and run a very simple test suite using it, check out this basic Cucumber example. It uses the capybara-webkit driver, but it should work with any driver that supports JavaScript execution.