Michael Schuerig
2010-Jul-03 20:34 UTC
[rspec-users] Loading problem with engine extending ActionController::Base
I''m working on an engine that extends ActionController::Base like this
module MyEngine
class Engine < Rails::Engine
config.after_initialize do
ActionController::Base.class_eval do
include MyEngine::ControllerExtension
end
end
end
end
This works in the development environment (rails server). It does not
work for specs. No matter if I run rake spec or rspec spec/some_spec.rb.
rake spec indirectly loads the application through this line in Rakefile
require File.expand_path(''../config/application'', __FILE__)
in the normal course of rake initialization. During this,
ActionController::Base is loaded and the #after_initialize callback of
MyEngine is called.
Then, when a spec is loaded, that in turn loads spec/spec_helper.rb,
containing this line
require File.dirname(__FILE__) + "/../config/environment" unless
defined?(Rails)
Which causes, among other things, a reload of ActionController::Base,
but this time the engine callback is *not* executed. Thus, the specs
blow up as soon as they run into a method defined in the engine. But,
wait, why is that require executed at all? Isn''t Rails defined at that
point? Apparently it is not and I have no idea why.
rspec spec/some_spec.rb is slightly different. Here
ActionController::Base is loaded just once, but the engine callback is
not executed.
rspec and associated gems are 2.0.0.beta.15
rails is 3.0.0.beta4
I''m stumped.
Michael
--
Michael Schuerig
mailto:michael at schuerig.de
http://www.schuerig.de/michael/
David Chelimsky
2010-Jul-03 20:43 UTC
[rspec-users] Loading problem with engine extending ActionController::Base
On Jul 3, 2010, at 3:34 PM, Michael Schuerig wrote:> I''m working on an engine that extends ActionController::Base like this > > module MyEngine > class Engine < Rails::Engine > > config.after_initialize do > ActionController::Base.class_eval do > include MyEngine::ControllerExtension > end > end > end > end > > This works in the development environment (rails server). It does not > work for specs. No matter if I run rake spec or rspec spec/some_spec.rb. > > > rake spec indirectly loads the application through this line in Rakefile > > require File.expand_path(''../config/application'', __FILE__) > > in the normal course of rake initialization. During this, > ActionController::Base is loaded and the #after_initialize callback of > MyEngine is called. > > Then, when a spec is loaded, that in turn loads spec/spec_helper.rb, > containing this line > > require File.dirname(__FILE__) + "/../config/environment" unless > defined?(Rails)This is from an old version of the spec_helper. Be sure to follow the post-install instructions (that''s what they''re there for) and run "script/rails generate rspec:install". The newer generated spec_helper uses File.expand_path for this require, so it doesn''t load the file a second time. I believe that will fix this issue for you. HTH, David> > Which causes, among other things, a reload of ActionController::Base, > but this time the engine callback is *not* executed. Thus, the specs > blow up as soon as they run into a method defined in the engine. But, > wait, why is that require executed at all? Isn''t Rails defined at that > point? Apparently it is not and I have no idea why. > > rspec spec/some_spec.rb is slightly different. Here > ActionController::Base is loaded just once, but the engine callback is > not executed. > > rspec and associated gems are 2.0.0.beta.15 > rails is 3.0.0.beta4 > > I''m stumped. > > Michael
Michael Schuerig
2010-Jul-03 21:40 UTC
[rspec-users] Loading problem with engine extending ActionController::Base
On Saturday 03 July 2010, David Chelimsky wrote:> On Jul 3, 2010, at 3:34 PM, Michael Schuerig wrote:[double loading of Rails framework classes]> > require File.dirname(__FILE__) + "/../config/environment" unless > > defined?(Rails) > > This is from an old version of the spec_helper. Be sure to follow the > post-install instructions (that''s what they''re there for) and run > "script/rails generate rspec:install".Indeed, you''re right. I compared old and new visually (only) and didn''t notice the difference. As I have some stuff in spec_helper.rb, I don''t like to overwrite it.> The newer generated spec_helper uses File.expand_path for this > require, so it doesn''t load the file a second time. I believe that > will fix this issue for you.Unfortunately and surprisingly not. I''m using ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux] in case that makes a difference. I can work around this by including the required extension explicitly in ApplicationController, of course. Michael -- Michael Schuerig mailto:michael at schuerig.de http://www.schuerig.de/michael/
David Chelimsky
2010-Jul-05 17:38 UTC
[rspec-users] Loading problem with engine extending ActionController::Base
On Jul 3, 2010, at 4:40 PM, Michael Schuerig wrote:> On Saturday 03 July 2010, David Chelimsky wrote: >> On Jul 3, 2010, at 3:34 PM, Michael Schuerig wrote: > > [double loading of Rails framework classes] >>> require File.dirname(__FILE__) + "/../config/environment" unless >>> defined?(Rails) >> >> This is from an old version of the spec_helper. Be sure to follow the >> post-install instructions (that''s what they''re there for) and run >> "script/rails generate rspec:install". > > Indeed, you''re right. I compared old and new visually (only) and didn''t > notice the difference. As I have some stuff in spec_helper.rb, I don''t > like to overwrite it. > >> The newer generated spec_helper uses File.expand_path for this >> require, so it doesn''t load the file a second time. I believe that >> will fix this issue for you. > > Unfortunately and surprisingly not.Did you try both ''rake spec'' and ''bundle exec rspec spec''?> I''m using > > ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux] > > in case that makes a difference. > > I can work around this by including the required extension explicitly in > ApplicationController, of course.Yeah, but who wants to do that! Would you do me a favor and raise this as an issue in http://github.com/rspec/rspec-rails/issues? I won''t be able to address this for a bit, and that''s the best way to ensure it stays on the radar. Cheers, David
Michael Schuerig
2010-Jul-05 20:21 UTC
[rspec-users] Loading problem with engine extending ActionController::Base
On Monday 05 July 2010, David Chelimsky wrote:> On Jul 3, 2010, at 4:40 PM, Michael Schuerig wrote: > > On Saturday 03 July 2010, David Chelimsky wrote: > >> On Jul 3, 2010, at 3:34 PM, Michael Schuerig wrote: > > [double loading of Rails framework classes] > > > >>> require File.dirname(__FILE__) + "/../config/environment" unless > >>> defined?(Rails) > >> > >> This is from an old version of the spec_helper. Be sure to follow > >> the post-install instructions (that''s what they''re there for) and > >> run "script/rails generate rspec:install". > > > > Indeed, you''re right. I compared old and new visually (only) and > > didn''t notice the difference. As I have some stuff in > > spec_helper.rb, I don''t like to overwrite it. > > > >> The newer generated spec_helper uses File.expand_path for this > >> require, so it doesn''t load the file a second time. I believe that > >> will fix this issue for you. > > > > Unfortunately and surprisingly not. > > Did you try both ''rake spec'' and ''bundle exec rspec spec''?bundle exec rspec spec does not make a difference.> Would you do me a favor and raise > this as an issue in http://github.com/rspec/rspec-rails/issues? I > won''t be able to address this for a bit, and that''s the best way to > ensure it stays on the radar.There you go: http://github.com/rspec/rspec-rails/issues/#issue/118 Michael -- Michael Schuerig mailto:michael at schuerig.de http://www.schuerig.de/michael/