[rspec-users] How to specify current directory for specs?

Zach Dennis zach.dennis at gmail.com
Tue Nov 9 18:12:35 EST 2010


On Tue, Nov 9, 2010 at 1:14 PM, Daniel Lidström <dlidstrom at gmail.com> wrote:

> Hello,
>
> I'd like to know if it's possible to specify what directory to use as
> the current directory when executing the specifications. My specs are
> in spec/ and I have some data in spec/data that I want to read from
> one of the specs:
>
> spec/book_spec.rb
>
> require 'book'
>

You can do this by ensuring the spec/ directory is at the front of your load
path, ie:

  $LOAD_PATH.unshift 'spec'
     or
  $:.unshift 'spec'

This will make it so when Ruby tries to require "book.rb" it looks in the
spec directory first. I wouldn't recommend this for loading spec helpers as
it will take precedent over application code and you may run into strange
issues.


> describe Book do
>  it "should read version" do
>    book = Book.new "data/JA_s12.book"     # This should open the file
>    book.version.should == 1
>  end
> end
>
> Of course I can prepend spec/ to the path above, but I'd rather not. I
> have created a rake task for running my specifications:
>
>
> desc "Run all specs in the spec directory"
> RSpec::Core::RakeTask.new('spec') do |t|
>  t.rspec_opts = %w[--color]
> end
>
> Can I specify the spec/ directory as the current directory when
> running this task? What is the expert recommendation? Is the data for
> my specs malplaced, you think?
>

Two things come to mind as how I might approach this: use spec_helper to
always load seed-data, or use a different mechanism to load your seed data.
(well there's a third, leave the require more verbose)

The first would be to open up spec_helper.rb and have it always load seed
data when rspec runs:

    Dir["#{File.dirname(__FILE__)}/data/**/*.rb"].each {|f| require f}

The second would be to create a custom method so in your spec you would say:

   seed_data 'book'

And then you can have seed_data know about the full path to require the
data, ie:

    def seed_data(name)
        require "spec/data/#{name}"
    end

As much as I like removing unnecessary requires in specs I prefer verbosity
over magic -- anything that gives the code reader more clarity as to what's
necessary for a particular example group.  A third option might be to make
this work for both your rake task and when running specs individually:

    require File.join(Rails.root, "spec/data/book")

Hope this helps,

Zach

Thanks in advance!
>
> Daniel
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



-- 
Zach Dennis
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
http://ideafoundry.info/behavior-driven-development (first rate BDD
training)
@zachdennis (twitter)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101109/ffebc9f4/attachment.html>


More information about the rspec-users mailing list