Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

rake

rspec

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

like image 285
emery Avatar asked Mar 01 '13 22:03

emery


2 Answers

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

like image 135
IgorM Avatar answered Oct 19 '22 06:10

IgorM


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
like image 23
ad-johnson Avatar answered Oct 19 '22 06:10

ad-johnson