Circular Dependency Error in Rails 4 during Rspec Request Specs
Asked Answered
R

1

15

I have an app running on Rails 4 / Ruby 2. The frontend is being developed with Ember.

When running rake spec, I'm getting the below exception. It seems to only happen in Rspec feature specs (not in dev mode). Also, it usually only happens if I run just the one feature spec; if I run the entire test suite, I don't usually get the error.

The error is occurring because of this commit to Rails: https://github.com/rails/rails/commit/b33700f5580b4cd85379a1dc60fa341ac4d8deb2

But, of course, I don't know if that's the real problem or if it's something deeper in the call stack. I do know, if I make a minimal change in the Rails code to not throw that error, everything seems to work fine and my tests pass. But something, somewhere, seems to be trying to load LocationsController even though it's already loaded.

Any help is greatly appreciated, as this one has me really stumped.

  1) Locations Creating locations
     Failure/Error: Unable to find matching line from backtrace
     RuntimeError:
       Circular dependency detected while autoloading constant LocationsController
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/dependencies.rb:460:in `load_missing_constant'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/dependencies.rb:183:in `const_missing'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:226:in `const_get'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:226:in `block in constantize'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:224:in `each'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:224:in `inject'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:224:in `constantize'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/dependencies.rb:534:in `get'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/dependencies.rb:565:in `constantize'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/routing/route_set.rb:76:in `controller_reference'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/routing/route_set.rb:66:in `controller'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/routing/route_set.rb:44:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/journey/router.rb:71:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/journey/router.rb:59:in `each'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/journey/router.rb:59:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/routing/route_set.rb:655:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/request_store-1.0.5/lib/request_store/middleware.rb:9:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/warden-1.2.1/lib/warden/manager.rb:34:in `catch'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/warden-1.2.1/lib/warden/manager.rb:34:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/etag.rb:23:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/conditionalget.rb:25:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/flash.rb:241:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:225:in `context'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/cookies.rb:486:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activerecord/lib/active_record/query_cache.rb:36:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/callbacks.rb:373:in `_run__1828229838678430325__call__callbacks'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/callbacks.rb:80:in `run_callbacks'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/rack/logger.rb:38:in `call_app'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/rack/logger.rb:21:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/tagged_logging.rb:67:in `block in tagged'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/tagged_logging.rb:25:in `tagged'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/tagged_logging.rb:67:in `tagged'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/rack/logger.rb:21:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/quiet_assets-1.0.2/lib/quiet_assets.rb:18:in `call_with_quiet_assets'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/request_id.rb:21:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/static.rb:64:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/engine.rb:511:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/application.rb:97:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/builder.rb:138:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/urlmap.rb:65:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/urlmap.rb:50:in `each'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/urlmap.rb:50:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/capybara-2.1.0/lib/capybara/server.rb:19:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
     # /usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
     # /usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
     # /usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

routes.rb:

get '/locations', to: 'main#index', :as => :em_locations

scope 'api' do
  resources :locations, only: [:index]
end

Relevant Gems:

  * capybara (2.1.0)
  * rspec (2.13.0)
  * rspec-core (2.13.1)
  * rspec-expectations (2.13.0)
  * rspec-mocks (2.13.1)
  * rspec-rails (2.13.2)
  * ember-data-source (0.0.5)
  * ember-rails (0.12.0)
  * ember-source (1.0.0.rc3.3)
  * active_model_serializers (0.8.1) 

What I've tried:

  • Changing the names and paths of my routes, thinking the ember /locations might be conflicting.
  • Changed a number of different Rails options I thought might be relevant. Actually, if I set config.cache_classes to false in the test.rb env file, it solves the problem albeit at the cost of making the specs run about 5X slower.

Here's the spec I'm running:

require 'spec_helper'

feature 'Locations', js: true, strategy: :truncation, slow: true do

  before do
    login_for_request
    Factory.create(:warehouse, code: "TW", name: 'TEST WAREHOUSE')
  end

  scenario "Creating locations" do
    visit em_locations_path
    click_link "create-locations"
    select 'TEST WAREHOUSE', from: "warehouse"
    select "GLD", from: "section"
    fill_in "row-start", with: "AA"
    fill_in "row-end", with: "AB"
    fill_in "bay-start", with: "1"
    fill_in "bay-end", with: "2"
    fill_in "tier-start", with: "1"
    fill_in "tier-end", with: "2"
    fill_in "subbay-start", with: "1"
    fill_in "subbay-end", with: "2"
    click_button "Create"
  end


end
Roseola answered 29/5, 2013 at 17:18 Comment(0)
R
11

I came up with one possible solution to this problem. It's probably not the best way, and I don't think it should be necessary, but nonetheless it works and works consistently.

In my feature spec, I require the associated controller:

require_relative '../../app/controllers/locations_controller'

It was also necessary to require the active_model_serializer in the controller:

require_relative '../serializers/location_serializer'

I certainly welcome better solutions. In the meantime, my specs are passing.

Roseola answered 30/5, 2013 at 13:39 Comment(4)
Thank you so much for this question and answer. I had the same issue with namespaced controller specs and this solution of require_relative-ing the controller file in worked for me.Reathareave
Works for me as well. I really wonder what the deal is with this problem!Sidle
Is there a relevant issue opened in the rails project? I was able to find one for namespaced controller specs: github.com/rails/rails/issues/13001, but nothing for the original question here.Aphanite
Yeah, explicitly requiring my controller and the model concern it involved fixed my issue. This is total mystery meat to me but it works for now. Thanks again for posting this!Fluster

© 2022 - 2024 — McMap. All rights reserved.