Why are my RSpec specs running twice?

2020-08-09 08:43发布

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']
end

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'
    end
  end
end

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.

8条回答
趁早两清
2楼-- · 2020-08-09 09:07

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
--color

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

查看更多
ら.Afraid
3楼-- · 2020-08-09 09:09

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

查看更多
Deceive 欺骗
4楼-- · 2020-08-09 09:10

Don't know if this fixes the problem, but you can use require 'spec_helper' instead of require File.dirname(__FILE__) + '/../spec_helper'

Also, 'spec/autorun' will require 'spec' for you.

The only other thing I can think of is that you have two spec tasks defined in your system. Is this a rails app? If so, make sure you're not duplicating a rake task that already exists in lib/rake/tasks.

HTH, David

查看更多
虎瘦雄心在
5楼-- · 2020-08-09 09:11

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

e.g.

.rspec

--color

spec_helper.rb

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

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

查看更多
成全新的幸福
6楼-- · 2020-08-09 09:11

Other cause, also in the spec_helper.rb is using the following code:

RSpec.configure do |config|
    #config stuff
end

The tests were executed only once after removing this code.

查看更多
劳资没心,怎么记你
7楼-- · 2020-08-09 09:13

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)

查看更多
登录 后发表回答