可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
I had this problem using zeus, and removing require 'rails/autorun'
from my spec_helper.rb
stopped it for me
回答2:
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
回答3:
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
Rails.application.load_tasks
Rake::Task["spec"].clear
RSpec::Core::RakeTask.new(:spec) do |t|
# your logic here
end
回答4:
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)
回答5:
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.
回答6:
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
回答7:
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.
回答8:
We had this issue today at my company and it was for another reason not noted in here: We were auto-loading some files which contained test logic. These were then being run when the code was required in the first place:
# Require support, concerns, lib, validators dirs
%w(support concerns lib).each do |dir|
Dir[Rails.root.join("spec/#{dir}/**/*.rb")].each { |f| require f }
end
The solution is to remove auto-loading code. This kind of code in Rails + tests is not very conventional, and usually points to poor file structure.