<?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>Kabisa Blog &#187; Patrick Baselier</title>
	<atom:link href="http://blog.kabisa.nl/author/patrick/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kabisa.nl</link>
	<description>The Ruby on Rails Experts</description>
	<lastBuildDate>Thu, 17 Jun 2010 11:13:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Running cucumber features with sunspot_rails</title>
		<link>http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/</link>
		<comments>http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 07:57:15 +0000</pubDate>
		<dc:creator>Patrick Baselier</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.kabisa.nl/?p=75</guid>
		<description><![CDATA[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:

Sunspot::Rails::Server.new.start
&#160;
at_exit do
  Sunspot::Rails::Server.new.stop
end

Also, make sure you&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>We wanted to integrate the <a href="http://github.com/outoftime/sunspot" target="_blank">sunspot_rails gem</a> into our cucumber features. I found a <a href="http://outoftime.lighthouseapp.com/projects/20339/tickets/59-cucumber-testing-and-solr_rails" target="_blank">ticket</a> where <a href="http://outoftime.lighthouseapp.com/users/24899" target="_blank">mat</a> adviced to call <strong>Sunspot::Rails::Server.start / stop</strong> in one of the available cucumber hooks.</p>
<p>Code found in <strong>features/support/env.rb</strong> is run when Cucumber begins and exits so this seemed the right place to start Solr:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">start</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">at_exit</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">stop</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Also, make sure you&#8217;ve added a cucumber environment to the <strong>config/sunspot.yml</strong> file; we copied the test-environment for that:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">test: <span style="color:#006600; font-weight:bold;">&amp;</span>amp;TEST
  solr:
    hostname: localhost
    port: <span style="color:#006666;">8981</span>
    log_level: WARNING
&nbsp;
cucumber:
  <span style="color:#006600; font-weight:bold;">&amp;</span>lt;<span style="color:#006600; font-weight:bold;">&amp;</span>lt;: <span style="color:#006600; font-weight:bold;">*</span>TEST</pre></div></div>

<p>Running cucumber will let all scenarios fail unfortunately and we got a &#8216;<strong>Connection refused &#8211; connect(2) (RSolr::RequestError)</strong>&#8216; in our console.</p>
<p>The reason for this is simple (after I stepped into the sunspot_rails gem to be honest): the <strong>Sunspot::Rails::Server.new.start</strong> method will eventually launch a Java Servlet Container (Jetty) with the Solr WAR and uses the port defined in your <strong>sunspot.yml</strong>.<br />
Now, starting up a server could take some time, so if cucumber&#8217;s features are run before the Solr server is up and running, the &#8216;Connection refused&#8217; sounds trivial then.<br />
First I added a sleep after starting up the server:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">start</span>
<span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>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 <em>http://localhost:8981/solr</em> to administering Solr. This we can use to find out if we&#8217;ve waited long enough and start our features. I&#8217;ve put this into a seperate class named <strong>CukeSunspot</strong> and created this file in the <strong>features/support</strong> directory:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;net/http&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CukeSunspot
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@server</span> = <span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> start
    <span style="color:#0066ff; font-weight:bold;">@started</span> = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
    <span style="color:#0066ff; font-weight:bold;">@server</span>.<span style="color:#9900CC;">start</span>
    up
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
  <span style="color:#9966CC; font-weight:bold;">def</span> port
    <span style="color:#0066ff; font-weight:bold;">@server</span>.<span style="color:#9900CC;">port</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> uri
    <span style="color:#996600;">&quot;http://0.0.0.0:#{port}/solr/&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> up
    <span style="color:#9966CC; font-weight:bold;">while</span> starting
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Sunspot server is starting...&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Sunspot server took #{'%.2f' % (Time.now - @started)} sec. to get up and running. Let's cuke!&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> starting
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      request = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">URI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">Errno::ECONNREFUSED</span>
      <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">CukeSunspot.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">start</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">at_exit</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">stop</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And voila, if you run cucumber, you scenario&#8217;s should run successfully. On my machine:</p>
<pre>Sunspot server is starting...
Sunspot server is starting...
Sunspot server took 3.74 sec. to get up and running. Let's cuke!</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
