title: Getting uninitialized constant (NameError) inside decorators when running rspec tests via rake
body: <p>In my app I use an engine (blogit) to which I want to add some changes / behaviours.</p>
  
  <p>I followed the guides on how to override engine controllers/models and added the following:</p>
  
  <h1>The Code</h1>
  
  <p>In <code>config/initializer/blogit.rb</code></p>
  
  <pre><code># Requires extension ruby files in lib/blogit.
  Dir[Rails.root.join("lib/blogit/*.rb")].each {|f| require f}
  </code></pre>
  
  <p>In <code>lib/blogit/engine.rb</code></p>
  
  <pre><code>module Blogit
    class Engine &lt; ::Rails::Engine
      isolate_namespace Blogit
  
      config.to_prepare do
        Dir.glob(Rails.root + "app/decorators/**/blogit/*_decorator*.rb").each do |c|
          require_dependency(c)
        end
      end
    end
  end
  </code></pre>
  
  <p>In <code>app/decorators/controllers/blogit/comments_controller_decorator.rb</code></p>
  
  <pre><code>Blogit::CommentsController.class_eval do
    def create
      Rails.logger.info "decorated controller action"
      # ... overridden stripped ...
    end
  end
  </code></pre>
  
  <p>In <code>app/decorators/models/blogit/comment_decorator.rb</code></p>
  
  <pre><code>Blogit::Comment.class_eval do
    belongs_to :user
  end
  </code></pre>
  
  <p>To be mentioned:</p>
  
  <ul>
  <li>I have also created a migration to add a user reference to the comments model, since my app uses devise and I only want logged_in users to be able to comment. (Therefore I don't need the standard behaviour, so I'm going to override it.)</li>
  </ul>
  
  <h1>The Problem</h1>
  
  <p>If I run <code>rake</code> I get the weird error:</p>
  
  <pre><code>/Users/Kassi/.rvm/rubies/ruby-1.9.3-p392-railsexpress/bin/ruby -S rspec ./spec/controllers/home_controller_spec.rb ./spec/models/user_spec.rb
  /Users/Kassi/demo/app/decorators/controllers/blogit/comments_controller_decorator.rb:3:in `&lt;top (required)&gt;': uninitialized constant Blogit::CommentsController (NameError)
  </code></pre>
  
  <p>However, if I run the first line that rake mentions by hand (<code>.../ruby -S ...</code>), all tests are being run successfully.</p>
  
  <p>In my project I'm using <em>guard</em> with <em>spork</em>. Running guard will also let the tests pass without any error.</p>
  
  <p>The app itself runs fine, i.e. it starts without errors and I'm able to comment as I want. My decorator action code is being executed.</p>
  
  <p><strong>So what's different when running <em>rake</em>?</strong> 
  <strong>Why does it break?</strong></p>
  
  <p>Note:</p>
  
  <ul>
  <li>Adding <code>require "blogit"</code> or <code>require "blogit/comments_controller"</code> doesn't help. It actually can't find the controller using <em>require</em>.</li>
  </ul>
  
  <h1>A Demo Application</h1>
  
  <p>Since this problem is part of a bigger project, I created a new app from scratch for testing purposes that contains only the relevant stuff: basic rails app, rspec, devise, blogit and the decorators.
  It can be found here: <a href="https://github.com/kassi/decorator_demo_rspec" rel="nofollow">https://github.com/kassi/decorator_demo_rspec</a> (git://github.com/kassi/decorator_demo_rspec.git)</p>
  
  <p>Another repo using testunit (which is working!) can be found here: <a href="https://github.com/kassi/decorator_demo_testunit" rel="nofollow">https://github.com/kassi/decorator_demo_testunit</a> (git://github.com/kassi/decorator_demo_testunit.git)</p>