Helpers aren't loaded #6

Closed
rchampourlier opened this Issue Apr 3, 2012 · 30 comments

Projects

None yet
@rchampourlier

I'm running some tests with Cucumber through spork with spork-rails 3.2.0 and spork 1.0.0rc2 and I cannot get my helper methods to be loaded.

  • Ruby is ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0]
  • Rails is 3.0.12

Everything is fine when:

  • I run with spork 1.0.0rc2 only (removing spork-rails from the Gemfile). This is not acceptable since spork does not reload the controllers anymore.
  • I run without spork at all.

I'm using many gems (Devise, FactoryGirl, Mongoid - also using ActiveRecord), but not ActiveAdmin. I say this because this issue is like the issue #109 on spork. Nothing from this issue could help me, and now that spork-rails has been extracted, it seemed to me the issue belonged here.

Here is a gist of my configuration. For now, I will be running my features without Spork...

@rchampourlier

Same issue when running integration/request specs within RSpec.

Any help? Thanks in advance... to anyone!

@mreinsch
mreinsch commented May 2, 2012

Same here. Seems any helpers coming from gems are not being loaded. All is fine when running tests directly...

@mreinsch
mreinsch commented May 2, 2012

ok, actually all helpers are not being available in the tests. If I disable the handling for reloading helpers (as in my branch at https://github.com/mreinsch/spork-rails), the helpers are fine.

Could this be an ordering issue where the helpers are somehow cleared again after getting loaded? I could confirm that they are being added, just not sure what happens then before the tests start.

@evgenyneu

I have the same problem. Devise helper devise_error_messages! is not available in tests and shows error:

undefined method `devise_error_messages!'

Devise includes this helper into DeviseController with method helper DeviseHelper (app/controllers/devise_controller.rb). spork-rails changes the helper method in app_framework/rails.rb, line 61. It them runs add_template_helper(mod) which does not work for DeviseHelper.

To make the Devise helper work, I needed to add the following code to spec_helper.rb

class DeviseController
  include DeviseHelper
  include ActionView::Helpers::TagHelper
  helper_method :devise_error_messages!
  helper_method :content_tag
end
@lidaobing

same issue as sporkrb/spork#109

@tom-kuca
tom-kuca commented Sep 7, 2012

Same issue with rails 3.2.8, spork 1.0.0rc3, spork-rails 3.2.0. I spent a few time trying to find out where the things went wrong.

Given a simple gem which defines module ApplicationHelper with method hello_world and given an application without ApplicationHelper in app/helpers/, the spec which tries to render view with hello_world method fails with undefined method hello_world.

Notes:
  • the gem does not include ApplicationHelper anywhere, it just defines it in it's lib directory
  • the spec passes without spork
  • the spec passes if there is even empty ApplicationHelper in app/helpers/application_helper.rb

I believe the problem is in the method helper. The spork version will raise an exception if ::ActiveSupport::Dependencies.search_for_file(filename) doesn't find the helper file in app/helpers while the original one from ruby AbstractController fallbacks to require 'application_helper'. Since the gem's lib directory is in $LOAD_PATH, the module from gem is loaded and the helper loads the helper methods.

Notes
  • maybe the gem should add helper's in some other way
  • when inheriting from ApplicationControlloer, the method helper is called with ApplicationHelper as an argument
@pedrobachiega

same problem to load helpers here...

if I include each helper, it works, but I would know some clean way to do this

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'shoulda/matchers/integrations/rspec'
  include ApplicationHelper
  include GraphicsHelper
  ...
@vascoosx
vascoosx commented Oct 7, 2012

I had the same problem. The only workaround I found was calling those helpers with

ApplicationController.helpers.<helper name> 
@fweep
fweep commented Nov 23, 2012

This problem still exists. I was digging around the source, and I think it has something to do with how spork-rails overrides the AbstractController::Helpers::ClassMethods#helper(), but I'm not sure, as I wasn't able to fix it there. I did figure out that if I just use spork instead of spork-rails, everything seems to work fine. I'm wondering what the benefit of spork-rails is. It doesn't do too much; disables eager loading (not sure why), loads some Rails environment stuff, and overrides #helper(). Everything runs nice and fast with just plain spork, though.

@krishnasrihari

I got same issue in spork, I tried in spork-rails gem but same issue. Is there any progress for this issue?

@krishnasrihari

To fix this issue add these below lines in Spork.prefork block

full_names = Dir["#{Rails.root}/app/helpers/*.rb"]
full_names.collect do |full_name|
    include Object.const_get(File.basename(full_name,'.rb').camelize)
end
@pedrobachiega

@krishnasrihari this works for me! thanks!

@eostrom
eostrom commented Dec 11, 2012

@krishnasrihari's workaround looks like it will cover helpers defined within the application, but not helpers in gems. It could be adapted, but really, my Spork configuration shouldn't have to go find all the gems with helpers in them.

I'm inclined to agree with @fweep's hunch that it has to do with the #helper override. The pull request for sporkrb/spork#140 fixes this problem by removing the override completely, but it was rejected because that caused other problems. (I.e., that code is there for a reason!)

The #helper override was added in sporkrb/spork@d08fee3. It looks like the goal was to ensure helpers are reloaded in each run, so you don't have to restart spork. Good idea, but is it somehow preventing helpers in gems from being loaded at all?

Oh hey, it looks like @tom-kuca's analysis has more details on this. Good job! Now all we need is a fix and an integration test.

@nozpheratu

I'm also having this problem...

@johnnyshields

Me too. +1

@krishnasrihari's fix worked for me.

@nozpheratu

The fixed that worked for me was:

AbstractController::Helpers::ClassMethods.module_eval do def helper(*args, &block); modules_for_helpers(args).each {|mod| add_template_helper(mod)}; _helpers.module_eval(&block) if block_given?; end end

I don't remember where I got this code, sorry for not giving any credit.

@n00dle n00dle referenced this issue in tablexi/nucore-open Mar 5, 2013
Merged

Load application helpers into environment #2

@marcusg
marcusg commented Apr 18, 2013

any progress here?

@yannp
yannp commented Jun 27, 2013

+1

@epylinkn
epylinkn commented Aug 3, 2013

+1

@sahilm
Contributor
sahilm commented Sep 14, 2013

Closing old bugs. If you think this issue needs resolution please open a new issue.

@sahilm sahilm closed this Sep 14, 2013
@johnnyshields

@sahilm why did you close this issue? It's still not fixed, despite many people requesting it.

@marcusg
marcusg commented Oct 29, 2013

@sahilm can you reopen this, because it is not fixed. thanks!

@brandonparsons

Yes this is still borked.

@jpemberthy

+1

@jalada
jalada commented Dec 3, 2013

👍

@daniel-g

Yes, it still doesn't work for me. I am using the following:

spork (1.0.0rc4)
spork-rails (4.0.0)
@johnnyshields

Workaround: use Zeus.

@TheNaoX
TheNaoX commented Dec 17, 2013

:(

@Arcolye
Arcolye commented Dec 27, 2013

I just installed Spork (+ guard) today, and I'm up against a bug that was first mentioned 2 years ago. Why closed?

@mihaivinaga

I found an easy fix, just add this in your spec_helper, Spork.prefork:

Dir[Rails.root.join("spec/helpers/*.rb")].each { |f| require f }

Also remove "require 'spec_helper'" from your helpers it will cause an error if it's there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment