<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>has_many :thoughts: Automagical RSpec: Shared Example Loading from Separate Files</title>
    <link>http://blog.kineticweb.com/articles/2008/04/15/automagical-rspec-shared-example-loading-from-separate-files</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Musings from a Ruby on Rails development team</description>
    <item>
      <title>Automagical RSpec: Shared Example Loading from Separate Files</title>
      <description>&lt;p&gt;I especially love Ruby because I can quickly customize it to my tastes and likes. With out regard for anyone else&amp;#8217;s feeling but my own. With that said, I do try and use this power for good. For the better of my office mates.&lt;/p&gt;


	&lt;p&gt;Earlier today I was doing some of this&amp;#8230;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_shell "&gt;describe Admin::ModelsController do
  shared_examples_for 'all admin pages' do
    code code code here...
  end

  describe 'when logged in' do
    more specific controller specs here...
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;...and I thought to myself that I would really just love to do this&amp;#8230;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_shell "&gt;describe Admin::ModelsController do
  it_should_behave_like 'all admin pages'

  describe 'when logged in' do
    more specific controller specs here...
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Loading it from some separated out module underneath the specific &lt;span class="caps"&gt;MVC&lt;/span&gt; spec/ sub directories.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ll explain more in a second&amp;#8230; but using some code I was working on before, thanks to storing it in Yojimbo, I quickly wrote this into my spec_helper.rb&amp;#8230;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_shell "&gt;Dir[File.dirname(__FILE__)+'/**/shared/*'].each { |group| 
  require group
  include Object.const_get(group.match(/.*[\/]{1}([\w]*)[.rb]./).captures.first.camelize)
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;As you can tell from the code, it will go through all sub-directories of the current one (RAILS_ROOT/spec in this case) and rummage for &amp;#8220;shared/&amp;#8221; directories. It will then try and load modules, within these files, named after the file&amp;#8217;s file name.&lt;/p&gt;


	&lt;p&gt;Without having to require and include each single file/module throughout your spec files (or the parent spec_helper).&lt;/p&gt;


	&lt;p&gt;Examples:&lt;/p&gt;


spec/controllers/shared/all_admin_pages.rb
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_shell "&gt;module AllAdminPages
  shared_examples_for 'all admin pages' do
    code code code here...
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Of course this is a smallish hack that I think really cleans out my specs. I generally got the idea from app/views/shared or app/views/layouts/shared directories in Rails. Keeping small shared view partials in separate, nicely organized sub-directories.&lt;/p&gt;


	&lt;p&gt;Let the flaming commence! j/k ;)&lt;/p&gt;</description>
      <pubDate>Tue, 15 Apr 2008 21:05:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:f91f1f27-f4a2-4167-b2a1-580d1905bc27</guid>
      <author>Justin Reagor</author>
      <link>http://blog.kineticweb.com/articles/2008/04/15/automagical-rspec-shared-example-loading-from-separate-files</link>
      <category>rspec</category>
      <category>ruby</category>
      <category>hacks</category>
    </item>
  </channel>
</rss>
