Rails and RSpec: `rake spec` fulfills all specifications twice

I created a new Rails 4 application using RSpec. But at startup, rake rspecall examples run twice:

rake spec
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
/Users/josh/.rvm/rubies/ruby-2.0.0-p353/bin/ruby -S rspec ./spec/controllers/dashboards_controller_spec.rb ./spec/models/member_spec.rb ./spec/requests/members_spec.rb ./spec/routing/members_routing_spec.rb
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
 11/11 |============================================ 100 ============================================>| Time: 00:00:00 

Finished in 0.21233 seconds
11 examples, 0 failures

Randomized with seed 15954

/Users/josh/.rvm/rubies/ruby-2.0.0-p353/bin/ruby -S rspec ./spec/controllers/dashboards_controller_spec.rb ./spec/models/member_spec.rb ./spec/requests/members_spec.rb ./spec/routing/members_routing_spec.rb
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
 11/11 |============================================ 100 ============================================>| Time: 00:00:00 

Finished in 0.18831 seconds
11 examples, 0 failures

Randomized with seed 24248

I found some other (old) questions about this, but could not find a solution for me. How should I try to debug this? The launch rspecworks like a charm, but I really want to know what the problem is.

Here's mine spec_helper.rb: https://github.com/jmuheim/transition/blob/master/spec/spec_helper.rb

And here is the original Rails project: https://github.com/jmuheim/transition

Refresh

I found that the specrake task seems to be defined twice (note the one /that shares the description of each task):

$ rake -T | grep spec
...
rake spec                               # Run all specs in spec directory (excluding plugin specs) / Run RSpec code examples
...

Run all specs in spec directory (excluding plugin specs), Run RSpec code examples.

Run RSpec code examples , rspec/core/rake_task.rb. Run all specs in spec directory (excluding plugin specs) , rspec/rails/tasks/rspec.rake.

, ?!

2

, , rspec-rails test development . : https://github.com/rspec/rspec-rails/issues/904

+5
6

- .rspec. , , .rspec .gitignore, , :

-- format progress
-- format documentation

rspec , .

+3
config.around(:each) do |example|
     ......

    DatabaseCleaner.start
    example.run # <================================= remove this
    DatabaseCleaner.clean
.......
  end

example.run spec. start clean, config.before(:each) config.after(:each) hooks

config.before(:each) do
  DatabaseCleaner.start
end

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

DatabaseCleaner

+2

, , . spec_helper.rb:

require 'rspec/autorun'
+2

, :

spec, Rakefile:

# http://blog.revathskumar.com/2011/12/run-rspec-as-rake-task.html
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task default: :spec

, . .

0

, RSpec rake, :default, Rakefile ( ):

require 'rspec/core/rake_task'
require 'cucumber/rake/task'

RSpec::Core::RakeTask.new
Cucumber::Rake::Task.new

task default: %i[spec cucumber]

RSpec ( ). :

task default: %i[spec cucumber]

.

Rakefile:

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks

task default: %i[spec cucumber]

FYI: Rails 4.2, RSpec 3 Cucumber 3.

0

CI

rspec spec Rails , RSpec CI. , :

bundle exec rake

RAILS_ENV. , spec_helper.rb ENV["RAILS_ENV"] ||= 'test'.

But on CI, ENV already has a RAILS_ENV=testsuite that also leads to the specRails task , which leads to double test runs.

Eventually:

bundle exec rake                  # => 1 test run
RAILS_ENV=test bundle exec rake   # => 2 test runs

Fix

Clean up task(:spec)after loadingRails.application.load_tasks (if you want to define the task yourself :spec).

diff --git a/Rakefile b/Rakefile
index 0f4fb7e0..94f7236a 100644
--- a/Rakefile
+++ b/Rakefile
@@ -6,14 +6,16 @@ require File.expand_path('../config/application', __FILE__)

 Rails.application.load_tasks

+task(:default).clear
+task(:spec).clear
+
 begin
   require "rspec/core/rake_task"
   RSpec::Core::RakeTask.new(:spec)
 rescue LoadError
 end

-task(:default).clear
 task default: [
   "factory_bot:lint",
   :spec,
 ]
0
source

All Articles