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

How to 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, is not very well documented; he just says:

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

 A new instance of RakeTask

What should I put for * args and & task_block?

I am following in the footsteps of someone who has already started building some ruby ​​automation for a PHP project using RSpec in combination with Rake. I'm used to using RSpec without Rake, so I'm not familiar with the syntax.

Thanks Kevin

+4
source share
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 you to run the specifications defined in spec.rb from Rake

+5
source

Here 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 test directory, and it will run all the tests with the name [something] _spec.rb - and it should work in different test directories (for example, in different projects); if you have the source in a separate directory (for example, in the code above, a subdirectory called "/ source", it will pick them up. Obviously, you can change this source directory to whatever you want.

Here's the rspec_config file I'm using, you can add your own settings here:

 RSpec.configure do |c| c.fail_fast = true c.color = true end 
+2
source

All Articles