Testing your Extension

Refinery ships with an optional testing extension in the form of a gem called refinerycms-testing which will add generators and rake tasks to your extension that provide it with the same RSpec2 testing environment we use to test Refinery. We abstract common test helpers, controller macros, and factories into this extension so you can use these in tests of your own.

This guide will show you how to:

  • Bootstrap your extension with the Refinery RSpec2 testing environment
  • Run the test suite with Rake for a one time run
  • Run the test suite with Guard for quick TDD iterations
Edit this guide on Github

1 Bootstrap the test environment

Make sure you perform all of the following steps from within your extension’s directory.

In your extension’s Gemfile uncomment or add the following line

gem 'refinerycms-testing'

And run

$ bundle install

You will need phantomjs from http://phantomjs.org/ or through your OS package manager (e.g. Ubuntu/Debian, OS X Homebrew, and npm below)

$ sudo apt-get install phantomjs
$ brew install phantomjs
$ npm install -g phantomjs

Now we will configure our test environment by preparing a dummy refinerycms app

Add the following lines to your extension’s Rakefile

ENGINE_PATH = File.dirname(__FILE__)
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)

load 'rails/tasks/extension.rake' if File.exists?(APP_RAKEFILE)

require "refinerycms-testing"
Refinery::Testing::Railtie.load_dummy_tasks ENGINE_PATH

And run the dummy application generator:

$ bin/rake refinery:testing:dummy_app

To successfully run rake tasks within your project you may need to add the refinerycms-testing gem to your application’s Gemfile, too.

2 Running the tests with Rake

$ bin/rake spec

This will run the rspec specs.

This is the most simple way to execute the test suite and useful for a one time run.

If you get Could not find table errors, it may help to delete the spec/dummy directory and run bin/rake refinery:testing:dummy_app again.

3 Running the tests with Guard

We recommend using Guard if you like to develop using TDD. Guard will watch your project for changes in files. If a change is made to a spec or a file that is tested against, it will re-run only the necessary specs. This is a faster way to get feedback during your TDD cycles.

At your extension’s root directory run:

$ bin/guard start

Larger Rails apps may take several seconds (or more) to start up. If that’s the case, you might also want to use Spork, which loads a single instance of the Rails environment and forks it to run tests, for even faster feedback cycles.