Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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 have found some other (old) questions about this, but couldn't find a solution for me. How should I try to debug this? Running rspec works like a charm, but I'm eager to find out what's the problem here.

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

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

Update

I found that the spec rake task seems to be defined twice (notice the / that separates the description of each task):

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

One of them is described with Run all specs in spec directory (excluding plugin specs), one with Run RSpec code examples.

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

It seems to me that only one of them should exist?!

Update 2

It seems to be a problem with rspec-rails being in both the test and development groups. I added an issue about this here: https://github.com/rspec/rspec-rails/issues/904

like image 358
Joshua Muheim Avatar asked Jan 01 '14 20:01

Joshua Muheim


2 Answers

For me this was due to a duplicate entry in my .rspec file. Can't tell if that was the problem here because the .rspec file's in the .gitignore but basically if you do this:

-- format progress
-- format documentation

In your rspec file you will see the tests output twice.

like image 195
Mikey Hogarth Avatar answered Oct 26 '22 23:10

Mikey Hogarth


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

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

you need to remove example.run call from your spec help. also, setup up your database cleaner start & clean methods using config.before(:each) & config.after(:each) hooks

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

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

so you start DatabaseCleaner BEFORE every test & clean AFTER every test

like image 38
CuriousMind Avatar answered Oct 26 '22 22:10

CuriousMind