<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spiral_code &#187; cucumber</title>
	<atom:link href="http://blog.trydionel.com/tag/cucumber/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.trydionel.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Aug 2010 00:35:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Testing Sunspot with Cucumber</title>
		<link>http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=testing-sunspot-with-cucumber</link>
		<comments>http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 19:07:43 +0000</pubDate>
		<dc:creator>Jeff Tucker</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[pickle]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sunspot]]></category>

		<guid isPermaLink="false">http://blog.trydionel.com/?p=124</guid>
		<description><![CDATA[I&#8217;ve been using Sunspot as my full-text search provider for a few months now. It&#8217;s a very enjoyable library &#8212; it hooks itself into Rails, provides an extension to disable itself in RSpec and even throws in a few convenient &#8230; <a href="http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using Sunspot as my full-text search provider for <a href="http://blog.trydionel.com/2009/11/19/a-few-sunspot-tips/">a few months now</a>.  It&#8217;s a very enjoyable library &#8212; it hooks itself into Rails, provides an extension to disable itself in RSpec and even throws in a few convenient rake tasks.  This fun came to a quick end, though, when I started delving into Cucumber again.  Any feature involving finding a Sunspot-enabled item failed completely, but without any helpful errors.  After a shameful amount of time banging my head against the desk, I finally got everyone to play nicely.</p>
<h2>The Code</h2>
<p>Let&#8217;s just drop the code in and discuss it afterwards:</p>
<pre class="brush: ruby;">
# config/sunspot.yml
# copy the test settings into the cucumber environment
test: &amp;TEST
  solr:
    hostname: localhost
    port: 8981
    log_level: WARNING

cucumber:
  &lt;&lt;: *TEST

# features/support/config.rb
# start the Solr server and give it a few seconds to initialize
Sunspot::Rails::Server.start
sleep 5

# make sure that pickle calls #index! on our appropriate models
require File.join(File.dirname(__FILE__), 'pickle')
module Pickle
  module Session

    def create_model_with_sunspot(a_model_name, fields = nil)
      result = create_model_without_sunspot(a_model_name, fields)

      model = model(a_model_name)
      model.index! if model.respond_to?(:index!)

      result
    end
    alias_method_chain :create_model, :sunspot

  end
end

# clean out the Solr index after each scenario
After do
  Post.remove_all_from_index!
end

# shut down the Solr server
at_exit do
  Sunspot::Rails::Server.stop
end
</pre>
</p>
<h2>The Details</h2>
<p>This method was heavily influenced by <a href="http://opensoul.org/2009/6/1/cucumber-scenarios-that-depend-on-sphinx">Brandon Keeper&#8217;s post on using Cucumber with ThinkingSphinx</a>.  Thanks Brandon!  So in my setup, I&#8217;m using Sunspot, Cucumber and Pickle (see Ryan Bates&#8217; <a href="http://railscasts.com/episodes/186-pickle-with-cucumber">railscast on using Pickle with Cucumber</a> for more details on the wonderful Pickle gem).</p>
<p>First and foremost is getting the testing Solr server running.  It will look for settings in <code>config/sunspot.yml</code> according to the environment we&#8217;re running in, so we need to duplicate the test environment for cucumber.  I&#8217;ve found that it takes 3-5 seconds for Solr to properly spin up, so we&#8217;re <code>sleep</code>ing for a few seconds to avoid the problem.</p>
<p>Next we need to ensure that each model gets loaded into Solr&#8217;s index when created.  If you&#8217;re instantiating models on your own, this boils down to calling <code>#index!</code> on any models that you create.  Since I&#8217;m using Pickle (and didn&#8217;t want to rework the included <code>pickle_steps.rb</code>), I needed a bit more invasive measure.  A bit of <code>alias_method_chain</code> voodoo lets me call <code>#index!</code> on every instantiated model that responds to it.  This ensures that Pickle always adds it&#8217;s models to Solr&#8217;s index.  I was pleased to find that I didn&#8217;t have to disable transactional fixtures </p>
<p>Finally, we need to ensure that our index is fresh for each scenario, so we clear the Solr index out after each scenario is run.  If you want a more targeted approach, you can use</p>
<pre class="brush: ruby;">
# features/support/config.rb
# clear Solr index only if we've actually used it
After('@sunspot') do
  Post.remove_all_from_index!
end
</pre>
<p>to only clear the Solr index after stories which are tagged with <code>@sunspot</code>.  Once all the stories are run, shut down the server.  Viola!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
