Why are my RSpec specs running twice?

rspec gem
run rspec
bundle install rspec
rspec --init
an error occurred while loading rspec
failure error require file join file dirname file shoulda
run rspec-rails
failure error: require spec_helper

I have the following RSpec (1.3.0) task defined in my Rakefile:

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
  spec.libs << 'lib' << 'spec'
  spec.spec_files = FileList['spec/**/*_spec.rb']

I have the following in spec/spec_helper.rb:

require 'rubygems'
require 'spec'
require 'spec/autorun'
require 'rack/test'
require 'webmock/rspec'

include Rack::Test::Methods
include WebMock

require 'omniauth/core'

I have a single spec declared in spec/foo/foo_spec.rb:

require File.dirname(__FILE__) + '/../spec_helper'

describe Foo do
  describe '#bar' do
    it 'be bar-like' do
      Foo.new.bar.should == 'bar'

When I run rake spec, the single example runs twice. I can check it by making the example fail, giving me two red "F"s.

One thing I thought was that adding spec to the SpecTask's libs was causing them to be double-defined, but removing that doesn't seem to have any effect.

I had this problem using zeus, and removing require 'rails/autorun' from my spec_helper.rb stopped it for me

RSpec Suite Runs Twice?, When running the specs via rake spec or rspec the whole suite would appear to run twice. I began digging around in spec/spec_helper.rb to try  RSpec when run in embedded mode via RSpec::Core::Runner reloads spec files itself. Loading it one more time before specs are run causes specs to run twice. Lib files still need to be reloaded explicitly.

Duplication configurations in spec_helper.rb and .rspec was causing my tests to run twice.





Rspec.configure do |config|
  config.color = true

So it appears to duplicate the test when you have the SAME configure setting duplicated in .rspec and spec_helper.rb

Running specs multiple times with different runner options in the , resets different counts of examples (all examples, pending and failed). require "​spec_helper" RSpec::Core::Runner.run([. Running specs multiple times with different runner options in the same process Use clear_examples command to clear all example groups between different runs in the same process.

Turns out Rails has default spec task, so if you are using Rails, the solution to the problem would be to clear pre-defined task and then re-initialize your custom logic, like this:

# ... beginning of Rails Rakefile


RSpec::Core::RakeTask.new(:spec) do |t|
  # your logic here

Everything in the config is loaded twice when testing using rspec , Everything in the config is loaded twice when testing using rspec #1694 Per your backtrace, your loading Rails in your rails_helper.rb and your group spec: all the initializing files are executed twice while running rspec. directories run once files run twice running lines in tests works. Without rspec/autorun directories never run (0 tests) files run once (correctly) running lines in tests doesn't work (not found, perhaps because 0 tests on the directory?) (zeus master, rspec 2.12.0) fwiw, when rspec runs twice, the second time my specs are failing due to database leftovers.

This is not directly related to the question as this apparently concerns Rspec versions from 2 onwards, but as the title of the question is the same that brought me here, I mention to others who might find this page for the same reason, that having rspec.rake in your lib/tasks can cause the rake spec to run all spec tests twice. Removing that file helped me (as was suggested at: http://www.patrickgannon.net/post/519eed022c17433fc8000018/rake-runs-rspec-tests-twice)

suggested config causes suite to run twice · Issue #2169 · rspec , Define spec as the default rake task in Rakefile, with a pattern specified for included _spec.rb files. # Rakefile require "rspec/core/rake_task"  Adding to what @iRonin said, I've noticed that if I have any option duplicated, it will run the tests twice. For example, if I have --tag ~js in my .rspec file in the project root, and --tag focus in my guardfile, it runs twice.

I observed a similar thing. But in my case it's not that the tests ran twice, they were printed twice.

The reason for this, I found out, is that I alias rspec to rspec -f d -c (formatting documentation and color).

And the spec_helper.rb contains

config.color = true
config.formatter = :documentation

So essentially this information is duplicated. After I run unalias rspec, I run my tests again, the problem is fixed.

You may also have a .rspec file that contains similar info:

--format d

This could also result in the duplication. So modify or move this file.

'Flaky' Tests: A Short Story - The Lean Software Boutique, It will run your test 10 times in a row, and hopefully you might see the test fail once or twice. RSpec's feedback should provide some insight into  A common reason for a controller to repeat a cycle is too many programmed start times. Only one start time per active program is required, a second start time will cycle the program an additional time.

5 Ways to Increase the Performance of Your RSpec Tests, If your test suite takes 25 minutes to run 2000 tests, running all tests as you make changes will not be productive. To help mitigate this, a two-fold  The specs are running twice because you have two tasks loaded and matched by Rake. Can you upload your Gemfile and Rakefile to gist.github.com? Seems like somehow the rspec rask is conflicting with rspec-rails here Also the reason you'd want rspec-rails in :development is so that you can use the generators, it's not overly important.

Rails and RSpec: `rake spec` runs all specs twice, I set up a new Rails 4 app using RSpec. But when running rake rspec , all the examples are run twice: rake spec [deprecated] I18n.enforce_available_locales  RSpec.configure do |config| config.filter_run :focus => true config.run_all_when_everything_filtered = true end To my spec_helper file. Now, whenever I want to run one specific test (or context, or spec), I can simply add the tag “focus” to it, and run my test as normal – only the focused test(s) will run.

Controller filters running twice in 1.1.4? - RSpec, be running twice. I created a test rails project with the rspec … loaded twice when they're being run with autotest/spec. To reproduce what What I'd like is to be able to do is set some sort of flag to tell RSpec to run the same spec twice w/ each filter set. Something like: it 'does this same thing with and without JS enabled', :run_with => [{:js => true}, {:js => false}] do # stuff here end

  • I do not have this line and I am still having this problem. Any other idea?
  • The best advice i can give you right now is to upgrade to rails 4.1, and get Spring built in, which will allow you to stop using Zeus altogether.
  • Having the .rspec file alone made it run twice, I didn't even need the duplicate settings
  • I had --color and --format d in my ~/.rspec and config.color = true and config.formatter = :documentation in my code. Had to get rid of the duplicates to not have rspec running twice (didn't matter if I removed from code or config file).
  • More info: "You're adding two formatters, so getting double the output", says rspec owner at github.com/rspec/rspec-core/issues/1929#issuecomment-90771361
  • It's not a Rails app, so that's not the problem. I can only do require 'spec_helper' if my $LOAD_PATH is already set up, but if I want to run a single spec, $LOAD_PATH won't get set until inside spec_helper.rb.
  • I had configuration code where you have the comment. Removing it made my tests run once. I'm not sure why. Looks like I'll need to use ~/.rspec instead.
  • I'm using factory_bot_rails. I has added a linting test under spec/factories/factory_bot_spec.rb. It was running twice. Once I moved it to spec/models/factory_bot_spec.rb it started running only once.