emery
emery

Reputation: 9723

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

Upvotes: 6

Views: 6968

Answers (2)

ad-johnson
ad-johnson

Reputation: 565

This is what my rakefile looks like

gem 'rspec', '~>3'
require 'rspec/core/rake_task'

task :default => :spec

desc "run tests for this app"
RSpec::Core::RakeTask.new do |task|
  test_dir = Rake.application.original_dir
  task.pattern = "#{test_dir}/*_spec.rb"
  task.rspec_opts = [ "-I#{test_dir}", "-I#{test_dir}/source", '-f documentation', '-r ./rspec_config']
  task.verbose = false
end

You can 'rake' from your tests directory and it will run all tests with a name [something]_spec.rb - and it should work across different test directories (e.g. different projects); if you have source in a separate directory (e.g. in the code above, a subdirectory called '/source' it will pick them up. Obviously, you can change that source directory to what you want.

Here's the rspec_config file I use - you can add your own settings in here:

RSpec.configure do |c|
  c.fail_fast = true
  c.color = true
end

Upvotes: 4

IgorM
IgorM

Reputation: 1078

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

Upvotes: 6

Related Questions