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

Rhett Sutphin rhett at detailedbalance.net
Wed Nov 10 16:50:17 EST 2010

Hi Daniel,

On Nov 10, 2010, at 2:59 PM, Daniel Lidström wrote:

> Hi Zach,
> thanks for your detailed answer. I think I need some clarifications: I
> am not using Rails, I have a regular ruby project. When I say I need
> to read a file, I am talking about a binary file with Othello
> positions, not a spec helper. It is not a file I can 'require-in'.

You can change the working directory for the process using FileUtils.cd.  If you want to change the directory for all your specs, you could do something like:

# In spec/spec_helper.rb
require 'fileutils'
# ...
RSpec.configure do |config|
    FileUtils.cd File.dirname(__FILE__)

You could do the same thing for particular specs if that works better with what you want.  Just adjust the path.

As an alternative, you might also consider having a helper like this:

# again, in spec/spec_helper.rb
def spec_path(path)
  File.expand_path("../spec", __FILE__)

which you would use like so:

book = Book.new spec_path("data/JA_s12.book")

This has the benefit of being more explicit / less surprising to other developers (including your future self), since the relative path for spec execution is usually the project root.


> Sorry for the confusion.
> Daniel
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list