10
votes


I refactored my controllers by introducing request and response models to do some of the logic that was hanging around the controllers following this presentation. I wrapped all the response and request models with a module Responses and Requests respectively. The applications runs perfectly but when I run tests, I get the error below.

Failure/Error: Unable to find matching line from backtrace
RuntimeError:
Circular dependency detected while autoloading constant Responses::FolderContentResponse

My directory structure is as follows:
  - app/
   - models/
     - responses/

Note: I have seen the questions related to this issue but, their issues didn't seem similar to mine. In my case it happens randomly, and only when running tests (RAILS TEST ENV), the application is working perfectly.

module Responses
  class ContentResponse
   include ActiveAttr::Model
   #some attributes
   #some methods
  end
end

module Responses
 class FolderContentResponse < ContentResponse
 end
end

The FolderContent response class inherits from ContentResponse which has more generic methods that FolderContent other content responses use.

1
Can you provide a minimal example (eg your folder_content_response.rb file, with everything that doesn't impact the error removed) ? - Frederick Cheung
@Fredrich folder_content_response.rb is just an old ruby class With a couple of methods in it. I do not think that the implementation in this class has anything to with why its not being autoloaded. More so only when running tests. I have edited the question with the a bit of the code ion the class - Mukiza Andrew
Howabout content_response.rb? is there any code not in method definitions (ie that runs when the class is loaded)? - Frederick Cheung
Apart from including the github.com/cgriego/active_attr (ActiveAttr). ContentResponse class is just an ordinary ruby class. One thing I have realized though is that when I stop spring (spring stop). The tests will pass normally. I am using rails 4.1.4 and ruby 2.1.1. Some people got this problem because they were running rails 4.0.0rc. - Mukiza Andrew
When you say it only happens during tests, is it during specs using capybara? If so, which capybara driver? - Frederick Cheung

1 Answers

13
votes

This sounds a lot like an issue found recently by Xavier Noria. In a nutshell, capybara boots your application in multithreaded mode for the test even though the setting to load all app code upfront is not activated (needed because require and friends are not threadsafe)

It's been fixed for rails 4.2, on earlier versions

config.allow_concurrency = false

in test.rb should do the trick