Question

I've been using Michael Hartl's Rails Tutorial to pick up Ruby on Rails and have recently been going through the new Rails 4.0 version of the tutorial. I've encountered an issue with Spork; I know that we're using a custom fork of Spork for Rails 4.0 compatibility, and that this may just be a different incompatibility, but I wanted to post my issue and see if I was just doing something wrong or if anyone had any ideas. Whenever I call RSpec while Spork is running I get an ActiveRecord exception, while if I call RSpec by itself my tests run successfully - an example terminal dump is below:

oren@VM:~/ruby_projects/test_app$ rspec
Exception encountered: #<ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished>
backtrace:
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activerecord-4.0.0/lib/active_record/migration.rb:792:in `current_version'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activerecord-4.0.0/lib/active_record/migration.rb:800:in `needs_migration?'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activerecord-4.0.0/lib/active_record/migration.rb:379:in `check_pending!'
/home/oren/ruby_projects/test_app/spec/spec_helper.rb:105:in `<top (required)>'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `block in load'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/spork-1.0.0rc3/lib/spork/run_strategy/forking.rb:11:in `block in run'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/spork-1.0.0rc3/lib/spork/forker.rb:21:in `block in initialize'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/spork-1.0.0rc3/lib/spork/forker.rb:18:in `fork'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/spork-1.0.0rc3/lib/spork/forker.rb:18:in `initialize'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/spork-1.0.0rc3/lib/spork/run_strategy/forking.rb:9:in `new'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/spork-1.0.0rc3/lib/spork/run_strategy/forking.rb:9:in `run'
/home/oren/.rvm/gems/ruby-2.0.0-p195@rails_4_0/gems/spork-1.0.0rc3/lib/spork/server.rb:48:in `run'
/home/oren/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/drb/drb.rb:1588:in `perform_without_block'
/home/oren/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/drb/drb.rb:1548:in `perform'
/home/oren/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/drb/drb.rb:1626:in `block (2 levels) in main_loop'
/home/oren/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/drb/drb.rb:1622:in `loop'
/home/oren/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/drb/drb.rb:1622:in `block in main_loop'

I can provide any files from my app upon request, I'm just not sure what would be the most useful. I basically followed the first few steps of Chapter 3 of the tutorial (setting up new app, adding StaticPages controller, setting up RSpec, and adding the first spec) and the instructions for setting up Guard and Spork (Sec. 3.6.2 and 3.6.3).

Thanks for your help!

Was it helpful?

Solution

Your spec/spec_helper.rb likely has issues. Replace what you have with https://github.com/railstutorial/sample_app_rails_4/blob/master/spec/spec_helper.rb and see if that gets rid of the issue.

OTHER TIPS

I had the same problem, but I can't understand what you mean by moving the 'existing environment' into the Spork.prefork block. As far as I can tell, Listing 3.37 is supposed to be a complete spec/spec_helper.rb file. However, that file did not work for me: I got the 'connection not established error'.

Edit: Ah, I finally see what you mean. In the version of the spec/spec_helper.rb file that wouldn't work for me, there is a huge block of comments at the end of the file that runs off the bottom of my text editor, and after those comments there is another prefork block hiding. So what you should do is copy Listing 3.37, then open spec/spec_helper.rb and 'select all', then paste.

On the other hand, the spec/spec_helper.rb file on github did work for me. I ran a diff on the two files, and the git hub version differs at the end of the prefork block:

Listing 3.37:

    config.order = "random"
    config.include Capybara::DSL
  end
end

github:

    config.order = "random"
    # Include the Capybara DSL so that specs in spec/requests still work.
    config.include Capybara::DSL

    # Disable the old-style object.should syntax.
    config.expect_with :rspec do |c|
      c.syntax = :expect
    end
  end
end

I don't understand how the added code has anything to do with connections, but after trying Listing 3.37 many times and getting the connection error, I changed the file to the github version, and I got this output:

$ time bundle exec rspec spec/requests/static_pages_spec.rb --drb
........

Finished in 0.19795 seconds
8 examples, 0 failures

Randomized with seed 27433


real    0m5.568s
user    0m3.617s
sys 0m0.832s

Then I changed spec/spec_helper.rb back to the version in Listing 3.37, and I got the connection error again. So Listing 3.37 just doesn't work (Edit: Yes it does, see initial edit).

Edit: Note: if you use Listing 3.37, you won't get the following errors:

Next, after doing a few more steps in the tutorial, I noticed that Guard was outputting test failures:

Failures:

  1) StaticPagesController GET 'home' returns http success
     Failure/Error: response.should be_success
     NoMethodError:
       undefined method `should' for #<ActionController::TestResponse:0x000001046dba00>
     # ./spec/controllers/static_pages_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) StaticPagesController GET 'help' returns http success
     Failure/Error: response.should be_success
     NoMethodError:
       undefined method `should' for #<ActionController::TestResponse:0x00000104700058>
     # ./spec/controllers/static_pages_controller_spec.rb:15:in `block (3 levels) in <top (required)>'

Finished in 0.24867 seconds
13 examples, 2 failures, 3 pending

Those failures are from auto generated test files that use a method called should(), which the github version of the spec/spec_helper.rb file disabled:

# Disable the old-style object.should syntax.
config.expect_with :rspec do |c|
  c.syntax = :expect
end

Earlier in the tutorial, we were running only the tests in one file:

spec/requests/static_pages_spec.rb

But now Guard and Spork are setup to run all the tests in all the test files.

To get rid of the test failures, I just commented out the code in:

/spec/controllers/static_pages_controller_spec

There is also some different colored output from Guard that says:

Pending:
...

The tutorial hasn't talked about it yet, but in a test you can just write 'pending', like this:

describe StaticPagesHelper do
  pending "add some examples to (or delete) #{__FILE__}"
end

and then when you run the tests, the output will remind you that you still have to write the test by displaying the 'Pending' output. If you look in:

spec/helpers/static_pages_helper_spec.rb

that's where that pending test comes from.

I was getting this same error. I think it comes from the fact that I was running guard and spork at the same time. If you are following the tutorial it never tells you to exit guard before setting up spork. If you do exit guard and just run spork and then this command, you will not get the error. The next step in the tutorial shows you how to run guard and spork at the same time.

I had this same problem and just fixed it a few minuets ago, everything was accurate in my spec/spec_helper.rb file, or so i thought. Turns out I had to delete this copy of the top of the file that was hidden at the bottom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top