Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to configure guard to not run request specs?

I'm using guard to run all my rails specs and its awesome. I've written a bunch of request specs that use capybara and selenium to test my pages javascripts by opening firefox and they are awesome as well however they tend to be slow and pull focus away from my editor while I'm typing.

Is there a way to configure guard to not run my request specs when it runs all and maybe asign a hot key to just run the request specs?

like image 728
Paul Raupach Avatar asked Feb 25 '12 16:02

Paul Raupach


2 Answers

Answering my own question incase other come across this:

rspec-rails can pass command line arguments to rspec via :cli. Additionally examples can be tagged in spec files and then rspec can be run to include or exclude those tagged examples.

Turns out I'm already tagging the examples I wanted to exclude with :js=>true, wich is how you get Selenium to fire up firefox.

describe "Post" do
  it "should be able to edit a post", :js=>true do
    # your test here
  end
end

I made two groups in my Guardfile one for none-javascript specs with :cli => "-t ~js" and another for spec that test javascript with :cli => "-t js". I also passed in the :all_after_pass => false for the javascript group.

here is my new guard file:

group 'none-javascript specs' do
  guard 'rspec', :version => 2, :cli => '-r rspec/instafail -f RSpec::Instafail -t ~js'  do

   watch(%r{^spec/.+_spec\.rb$})
   watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
   watch('spec/spec_helper.rb')  { "spec" }

   # Rails example
   watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
   watch(%r{^app/(.*)(\.erb|\.haml|\.jbuilder)$})      { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
   watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
   watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
   watch('config/routes.rb')                           { "spec/routing" }
   watch('app/controllers/application_controller.rb')  { "spec/controllers" }

   watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }
  end
end

group 'javascript specs' do
  guard 'rspec', :version => 2, :all_after_pass => false, :cli => '-r rspec/instafail -f RSpec::Instafail -t js' do

    watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }
    watch(%r{^spec/requests/.+_spec\.rb$})
  end
end

Now when I start up guard or hit return in the guard term both groups are run executing all specs.

Guard::RSpec is running, with RSpec 2!
Running all specs
Run options: exclude {:js=>true}
...
Finished in 75.78 seconds
428 examples, 0 failures, 1 pending

Guard::RSpec is running, with RSpec 2!
Running all specs
Run options: include {:js=>true}
...
Finished in 63.68 seconds
22 examples, 0 failures

After all test pass only the none-javascript examples are run.

like image 99
Paul Raupach Avatar answered Nov 15 '22 08:11

Paul Raupach


There's an exclude option for guard-rspec. It was implemented in #23, but wasn't documented in the README. I've made a pull request to document that.

Example

guard 'rspec', :exclude => "spec/foo/**/*"  # exclude files based on glob
like image 38
Benjamin Oakes Avatar answered Nov 15 '22 09:11

Benjamin Oakes