Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to an Rspec SpecTask

Tags:

ruby

rake

rspec

Rake allows for the following syntax:

task :my_task, :arg1, :arg2 do |t, args|
  puts "Args were: #{args}"
end

I'd like to be able to do the same, but with RSpecs SpecTask.

The following unfortunately fails:

desc "Run example with argument"
SpecTask.new('my_task'), :datafile do |t, args|
  t.spec_files = FileList['*_spec.rb -datafile=#{args}']
  t.spec_opts = ["-c -f specdoc"]
end

Is it possible to achieve this with a SpecTask, or is there an alternative approach?

like image 730
Bayard Randel Avatar asked May 28 '10 02:05

Bayard Randel


1 Answers

if rspec doesn't support the args variable, you could pass it in as a command line parameter and/or a variable from another location.

rake datafile=somevalue

@datafile = ENV["datafile"]

desc "Run example with argument"
SpecTask.new :my_task do |t|
  t.spec_files = FileList["*._spec.rb -datafile=#{@datafile}"]
  #... etc
end
like image 168
Derick Bailey Avatar answered Sep 23 '22 01:09

Derick Bailey