Rails: Why does RSpec crash on my overridden Devis

2019-04-16 19:37发布

问题:

Update: Oh good grief, it was all a red herring following a bad merge. Somewhere along the line, the following for elided from a cleanup of spec/spechelper.rb

Dir["#{Rails.root}/app/**/*.rb"].each {|f| load f}

Now that this is restored, everything is working again. I suspect it was only load the flat dir, and not nested dirs, making it look like a namespace issue.

(I can't answer my own question as this is a new account, and I don't have the rep)


Original question:

Most of my RSpec/Capybara tests work fine, but I have one test that explodes on simply trying to visit a path, and I'm stuck on debugging. I suspect it's a Namespace issue but I don't know how to address that; any help gratefully received.

The entire test is:

require 'spec_helper'
describe 'Registrations' do
  context 'when debugging the blocking issue' do
    it 'this minimal test should pass' do
      visit new_user_registration_path
    end
  end
end

The error message is:

$ bin/rspec spec/requests/users/registrations_spec.rb

Failures:

1) Users::Registrations when debugging the blocking issue this minimal test should pass Failure/Error: visit new_user_registration_path LoadError: Expected app/controllers/users/registrations_controller.rb to define Users::RegistrationsController # ./spec/requests/users/registrations_spec.rb:10:in `block (3 levels) in '

So the error is Expected users/registrations_controller.rb to define Users::RegistrationsController, that's not much help!

The controller is an overridden Devise controller, and it looks like:

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    super
    # My extra code here
  end
end

My routes redirect devise a little:

devise_for :users, :controllers => { :registrations => 'users/registrations' } do
  get '/users/sign_out' => 'devise/sessions#destroy'
end

I'm utterly stuck on trying to find a root cause for the 'Expected foo.rb to define Foo' error'; I can't get an insight into what's really causing my issue.

What would you do next to debug this?


David suggested a backtrace might help; as far as I can see, I don't have an error preventing loading, looks to me (but I'm a rails noob) like all ActiveSupport blocks. Here it is for interest:

  1) Users::Registrations when debugging the blocking issue this minimal test should pass
     Failure/Error: visit new_user_registration_path

     LoadError:
       Expected app/controllers/users/registrations_controller.rb to define Users::RegistrationsController
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:490:in `load_missing_constant'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:181:in `block in const_missing'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:179:in `each'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:179:in `const_missing'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/inflector/methods.rb:124:in `block in constantize'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/inflector/methods.rb:123:in `each'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/inflector/methods.rb:123:in `constantize'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:528:in `block in initialize'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:550:in `yield'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:550:in `default'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:550:in `[]'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/routing/route_set.rb:61:in `controller_reference'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/routing/route_set.rb:46:in `controller'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/routing/route_set.rb:25:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/routing/mapper.rb:41:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:152:in `block in call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:96:in `block in recognize'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:110:in `optimized_each'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:95:in `recognize'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:141:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/routing/route_set.rb:531:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/sass-3.1.12/lib/sass/plugin/rack.rb:54:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/warden-1.1.0/lib/warden/manager.rb:35:in `block in call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/warden-1.1.0/lib/warden/manager.rb:34:in `catch'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/warden-1.1.0/lib/warden/manager.rb:34:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/etag.rb:23:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/conditionalget.rb:25:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/head.rb:14:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/flash.rb:243:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/session/abstract/id.rb:195:in `context'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/session/abstract/id.rb:190:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/cookies.rb:326:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activerecord-3.1.0/lib/active_record/query_cache.rb:62:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/callbacks.rb:28:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/sendfile.rb:101:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/railties-3.1.0/lib/rails/rack/logger.rb:13:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/methodoverride.rb:24:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/runtime.rb:17:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/activesupport-3.1.0/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/lock.rb:15:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/actionpack-3.1.0/lib/action_dispatch/middleware/static.rb:53:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/railties-3.1.0/lib/rails/engine.rb:455:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/urlmap.rb:52:in `block in call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/urlmap.rb:46:in `each'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-1.3.5/lib/rack/urlmap.rb:46:in `call'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-test-0.6.1/lib/rack/mock_session.rb:30:in `request'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-test-0.6.1/lib/rack/test.rb:219:in `process_request'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rack-test-0.6.1/lib/rack/test.rb:57:in `get'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/capybara-1.1.2/lib/capybara/rack_test/browser.rb:62:in `process'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/capybara-1.1.2/lib/capybara/rack_test/browser.rb:21:in `visit'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/capybara-1.1.2/lib/capybara/rack_test/driver.rb:32:in `visit'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/capybara-1.1.2/lib/capybara/session.rb:157:in `visit'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/capybara-1.1.2/lib/capybara/dsl.rb:161:in `visit'
     # ./spec/requests/users/registrations_spec.rb:10:in `block (3 levels) in '
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example.rb:51:in `instance_eval'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example.rb:51:in `block in run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example.rb:110:in `with_around_hooks'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example.rb:48:in `run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:306:in `block in run_examples'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:302:in `map'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:302:in `run_examples'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:274:in `run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:275:in `block in run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:275:in `map'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:275:in `run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:24:in `map'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:24:in `block in run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/reporter.rb:12:in `report'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:21:in `run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in `run_in_process'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:66:in `rescue in run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:62:in `run'
     # .rvm/gems/ruby-1.9.3-p0@foo/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in `block in autorun'

回答1:

If you type bin/rspec --help, you'll see a list of options and switches you can add to the command, including the --backtrace switch, which tells RSpec to show you the full backtrace. If you run bin/rspec spec/requests/users/registrations_spec.rb --backtrace you should see precisely where the error is coming from.

HTH, David



回答2:

Update: Oh good grief, it was all a red herring following a bad merge. Somewhere along the line, the following for elided from a cleanup of spec/spechelper.rb

Dir["#{Rails.root}/app/**/*.rb"].each {|f| load f}

Now that this is restored, everything is working again. I suspect it was only load the flat dir, and not nested dirs, making it look like a namespace issue.