Features

Testing and Local Development Using Split's Off-the-Grid Mode

Patricio Echague on December 6, 2016

This blog post will try to shine some light on the recently added support for local development as well as how to integrate Split into your continuous build and unit testing. We'll walk you through the creation of a demo app and how to run unit tests with Split by setting the expected behavior with a specific file in the path (we call it off-the-grid mode).

Creating a Demo App

Let's start with creating a rails app from the ground up:

Don't forget to add the splitclient-rb gem to your brand new Gemfile:

And run bundle install

Configuring Split

To configure the Split SDK you'll need to create an initializer:

Your splitclient.rb should look like this:

path required : the absolute path to the split file (more on that below)

reload_rate optional : the SDK can refresh features from the split file (it doesn't do so by default); here you can specify the reload rate in seconds.

Usage

Let's generate a sample WelcomeController with one action, index:

We also need to create the Split file. It can be located anywhere in the system, but for the sake of this tutorial let's put it in the config/ folder:

Make sure you include the full path to the file in the splitclient.rb initializer.

In your welcome_controller.rb:

And in the views/welcome/index.html.erb:

Now we can start our rails server by running:

Now when you visit http://localhost:3000/welcome/index?feature=awesome_feature you should see:

Treatment for the feature awesome_feature is on

And when you visit http://localhost:3000/welcome/index?feature=not_so_awesome_feature, you should expect to see:

Treatment for the feature awesome_feature is off

Any other feature name, which is not specified in the Split file should return control.

Changes picked up automatically

Just as developers make changes to their application and expect them to appear without refreshing the app, Split's off-the-grid mode supports exactly the same use case. Any changes made to the split file will be picked up in reload_rate seconds and the Split SDK will return the newly changed treatments, thus, providing a more interactive development experience without the need to reload your application server.

Testing

Let's write a test to make sure everything works as expected based on the content of our split off-the-grid mode file. Just to recap, our file looked like this:

Firstly, add rspec-rails gem to the Gemfile:

And run:

Inside splitio-test/spec/controllers/welcome_controller_spec.rb we'll write couple of specs:

The first one will expect we get on treatment for awesome_feature

The second one will test that we get off for not_so_awesome_feature:

And the last one will assert we get control on random_feature, which was not listed inside the split file:

Now run bundle exec rspec spec/controllers/welcome_controller_spec.rb

All three specifications should be green.

Wrap up

Split provides off-the-grid capabilities to perform split evaluations disconnected from our backend. The immediate consequence of that is that ability to perform unit tests without needing to do any remote calls to Split backend, as the treatments are stored in a file. This technique can be utilized for quick development as well as unit testing. For more sophisticate testing consider defining different configuration scenarios, each of them represented by a specific split off-the-grid file, and switching them in the Before section of your unit tests.


We're Hiring!

Join the Team