Search code examples
rubyrspecrake

How to create an RSpec Rake task using RSpec::Core::RakeTask?


How do I initialize an RSpec Rake task using RSpec::Core::RakeTask?

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new do |t|
  # what do I put in here?
end

The Initialize function documented at http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method isn't very well-documented; it just says:

 - (RakeTask) initialize(*args, &task_block)

A new instance of RakeTask

What should I put for *args and &task_block?

I'm following in the footsteps of someone who had already started to build some ruby automation for a PHP project using RSpec in combination with Rake. I'm used to using RSpec without Rake, so I'm unfamiliar with the syntax.

Thanks, -Kevin


Solution

  • Here is an example of my Rakefile:

    require 'rspec/core/rake_task'
    
    task :default => [:spec]
    
    desc "Run the specs."
    RSpec::Core::RakeTask.new do |t|
      t.pattern = "spec.rb"
    end
    
    desc "Run the specs whenever a relevant file changes."
    task :watch do
      system "watchr watch.rb"
    end
    

    This allows to run specs defined in the spec.rb file from Rake