2

I have »buildr« »buildfile« which triggers some »rspec« tests. I would like to pass some path parameters to the tests, so that It wont cause trouble to load test-resources files. In the »buildfile« I have got this code to trigger the tests:

RSpec.configure do |config|
    config.add_setting :spec_resources_dir, :default => _(:src, 'spec', 'ruby', 'resources')
end

RSpec::Core::RakeTask.new(:run_rspec) do |t|
  t.pattern = 'src/spec/**/*_spec.rb'
end
task test => [:run_rspec]

But if I try to retrieve the value in the specfile like this:

RSpec.configuration.spec_resources_dir

I get this error

undefined method `spec_resources_dir' […] (NoMethodError)

Any ideas?

philipp
  • 15,947
  • 15
  • 61
  • 106

1 Answers1

1

RSpec's rake task runs the specs in a separate process, so configuration you do with RSpec.configure in the buildfile will not be visible to the running specs.

Two suggestions for passing info from the buildfile to your spec task:

  1. Generate a spec_helper and require it from your specs (or via rspec's -r command line option and the rspec_opts config parameter on RSpec::Core::RakeTask). You could use buildr's filtering to substitute values from the buildfile into the helper.
  2. Set values in ENV and then read them out from your specs. Environment variables are shared from parent to child processes.

By request, an example for #1:

RSpec::Core::RakeTask.new do |t|
  t.rspec_opts = "-r '#{_(:target, 'spec_helper.rb')}'"
end

This assumes that you (probably in another task) generate the spec helper into _(:target, 'spec_helper.rb')

Rhett Sutphin
  • 1,045
  • 8
  • 15
  • that looks very much like the answer! Could please drop a line of code to show how to use the `rspec_opts` of the rake task, I am new to ruby, rspec and so on, and the documentation does not show an example… Thanks in ahead! – philipp Jan 17 '14 at 18:00
  • Thank You, very kind to post the example! – philipp Jan 21 '14 at 10:24