Running cucumber features with sunspot_rails

Patrick Baselier

Geschreven door Patrick Baselier op

We wanted to integrate the sunspot_rails gem into our cucumber features. I found a ticket where mat adviced to call Sunspot::Rails::Server.start / stop in one of the available cucumber hooks.

Code found in features/support/env.rb is run when Cucumber begins and exits so this seemed the right place to start Solr:

at_exit do

Also, make sure you’ve added a cucumber environment to the config/sunspot.yml file; we copied the test-environment for that:

test: &TEST
    hostname: localhost
    port: 8981
    log_level: WARNING

  <<: *TEST

Running cucumber will let all scenarios fail unfortunately and we got a ‘Connection refused – connect(2) (RSolr::RequestError)‘ in our console.

The reason for this is simple (after I stepped into the sunspot_rails gem to be honest): the method will eventually launch a Java Servlet Container (Jetty) with the Solr WAR and uses the port defined in your sunspot.yml.
Now, starting up a server could take some time, so if cucumber’s features are run before the Solr server is up and running, the ‘Connection refused’ sounds trivial then.
First I added a sleep after starting up the server:

and this will work for most cases, but I thought it would be better to let cucumber wait just long enough until the server is up and running. When the server is up, you can open up your browser and enter http://localhost:8981/solr to administering Solr. This we can use to find out if we’ve waited long enough and start our features. I’ve put this into a seperate class named CukeSunspot and created this file in the features/support directory:

require "net/http"

class CukeSunspot

  def initialize
    @server =

  def start
    @started =

  def port

  def uri

  def up
    while starting
      puts "Sunspot server is starting..."
    puts "Sunspot server took #{'%.2f' % ( - @started)} sec. to get up and running. Let's cuke!"

  def starting
      request = Net::HTTP.get_response(URI.parse(uri))
    rescue Errno::ECONNREFUSED


Now we can the alternative start method and remove the explicit sleep statement in our global before hook. features/support/env.rb:

at_exit do

And voila, if you run cucumber, you scenario’s should run successfully. On my machine:

Sunspot server is starting...
Sunspot server is starting...
Sunspot server took 3.74 sec. to get up and running. Let's cuke!


  1. Frank zegt:

    I’m sorry but I don’t see how you call your custom class. The last code at the bottom is calling the Sunspot class, not the CukeSunspot one so I don’t think your class will be used. Or did I miss something?

  2. […] nesse post, eu adaptei para o código que uso ao subir o sunspot com linha de comando para os testes de […]

  3. I had been using this code for some time when all of a sudden I started running into issues with rspec running twice and it caused all kinds of chaos.

    I came across this thread: but Andrew has not posted his monkeypatch, so I wrote one of my own.

  4. Tom-Eric zegt:

    I’ve improved the code a bit so it will only start solr when the @search tag is first found. This speeds up running individual feature files without any negative consequences:

  5. […] my solr server to load every time I started my test environment and after a bit of searching found this blog post about auto starting solr for cucumber and rspec. You will need to require the class in your env.rb […]

  6. […] first used this code and moved it into a lib/sunspot_starter.rb […]

  7. Hi there, thanks for the code I have been using for quite a while without any issues. Cheers!

  8. Venkatareddy zegt:

    I am using your this from a long time. Now i want to use parallel_tests for running cucumber. The problem being, for running multiple firefox windows it appears to me that i should run two sunspot instances for two separate processes. Is it possible to run two sunspot instances at the same time in the test environment?

Schrijf een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *

zeven + = 11

RT @pixelambacht Working on getting the programme on the site, and reading all the talk topics. Gosh DARN this is going to be sweet!