run rspec tests from specific folder using rake

capybara, rake, rspec

Solution

Why not just use rspec?

rspec spec/folder_A

UPDATED RESPONSE

The `:spec` in your `Rakefile` refers to the Rspec rake task, not the folder. You can send options to the task by passing a block as shown on the rake-task doc page

In your case you can pass in a glob for the folder using the `pattern` option.

RSpec::Core::RakeTask.new(:spec) do |t|
  t.pattern = 'spec/folder_A/*/_spec.rb'
end

For two different rake tasks you'll need to instantiate your `RakeTask` within each of your own tasks. So your entire `Rakefile` would look something like this:

require 'rspec/core/rake_task'

task :folder_A do
  RSpec::Core::RakeTask.new(:spec) do |t|
    t.pattern = 'spec/folder_A/*/_spec.rb'
  end
  Rake::Task["spec"].execute
end

task :folder_B do
  RSpec::Core::RakeTask.new(:spec) do |t|
    t.pattern = 'spec/folder_B/*/_spec.rb'
  end
  Rake::Task["spec"].execute
end

task :default do
  RSpec::Core::RakeTask.new(:spec)
  Rake::Task["spec"].execute
end

See the RakeTask doc for details on the `pattern` method and other options.

Problem

I wish to use rake to run specific tests in a folder contained withing spec folder. My folder structure is as follows: ``` - tests -spec - folder_A - folder_B - rakefile ``` So for example when certain code is deployed I just wish to run the tests in folder_A. How do I do this using rake? My rakefile lives in my tests folder. I currently have the command: ``` RSpec::Core::RakeTask.new(:spec) task :default => :spec ``` This runs all tests in the spec folder as you would expect. I have tried moving the rake file into the spec folder and editing the rake task to this: ``` RSpec::Core::RakeTask.new(:folder_A) task :default => :folder_A ``` However this gives me the message: "No examples matching ./spec{,//*}/*_spec.rb could be found" (note that within folders A and B I have sub directories for the different areas of the application under test) Is it possible for me to have 2 different rake tasks in the same rakefile that will run the tests just from folder_A? Any help would be great!!

Original source