Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guard runs my specs twice

I'm pretty sure Guard is the culprit, as running rspec runs all specs once. Running guard and enter results in all specs being run twice. Not sure why. Googled to destruction, and common gotchas, such as requiring 'rspec/autorun' in the spec_helper.rb are simply not the cause of it.

It can get quite annoying when the specs are starting to get slow!

.rspec

--color
--order default

Guardfile

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard :rspec 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|\.slim)$})          { |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" }

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

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    Capybara.run_server = false
    Capybara.javascript_driver = :webkit
    Capybara.default_selector = :css
    Capybara.server_port = 7171
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # config.include RSpec::Rails::RequestExampleGroup, type: :feature

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

Update

This may give a clue

$ guard
06:40:31 - INFO - Guard is using GNTP to send notifications.
06:40:31 - INFO - Guard is using TerminalTitle to send notifications.
06:40:31 - INFO - Guard::RSpec is running
06:40:31 - INFO - Guard::RSpec is running
06:40:31 - INFO - Guard is now watching at '/Users/starkers/Desktop/boxshare'
[1] guard(main)> 
06:40:35 - INFO - Run all
06:40:35 - INFO - Running all specs

Notice the two info logs at 06:40:31 that Rspec is running...

like image 921
Starkers Avatar asked Nov 25 '13 20:11

Starkers


3 Answers

FWIW, I had this problem too. Guard was running all of my rspec tests twice. It turns out that I had run guard init rspec twice and my Guardfile had the guard ... do ... end stanza duplicated.... doh.

like image 166
Stephen Henderson Avatar answered Oct 29 '22 22:10

Stephen Henderson


I've seen this issue several times. The problem is often (but not always) caused by a duplicated RSpec setting. Try removing the config.order = "random" line from spec_helper.rb, since you already have that setting in your .rspec file.

like image 3
infused Avatar answered Oct 29 '22 22:10

infused


My problem was with option all_after_pass.

Just changed my guard command on Guardfile to guard :rspec, cmd: 'rspec', all_after_pass: false do and it stopped.

https://groups.google.com/forum/#!topic/guard-dev/t1xxZpI5oWA https://github.com/guard/guard-rspec#list-of-available-options

like image 1
Rafael Oliveira Avatar answered Oct 29 '22 21:10

Rafael Oliveira