sporkrb / spork-rails

Spork plugin for rails
MIT License
184 stars 45 forks source link

Helpers aren't loaded #6

Closed rchampourlier closed 11 years ago

rchampourlier commented 12 years ago

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.

Everything is fine when:

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 commented 12 years ago

Same issue when running integration/request specs within RSpec.

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

mreinsch commented 12 years ago

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

mreinsch commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

same issue as https://github.com/sporkrb/spork/issues/109

tom-kuca commented 12 years ago

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:

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
pedrobachiega commented 12 years ago

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 commented 12 years ago

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

ApplicationController.helpers.<helper name> 
fweep commented 12 years ago

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 commented 12 years ago

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

krishnasrihari commented 12 years ago

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 commented 12 years ago

@krishnasrihari this works for me! thanks!

eostrom commented 11 years ago

@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@d08fee3b64d94c43a9f6347fb89e3e8f2311f691. 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 commented 11 years ago

I'm also having this problem...

johnnyshields commented 11 years ago

Me too. +1

@krishnasrihari's fix worked for me.

nozpheratu commented 11 years ago

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.

marcusg commented 11 years ago

any progress here?

yannp commented 11 years ago

+1

epylinkn commented 11 years ago

+1

sahilm commented 11 years ago

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

johnnyshields commented 11 years ago

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

marcusg commented 11 years ago

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

brandonparsons commented 11 years ago

Yes this is still borked.

jpemberthy commented 11 years ago

+1

jalada commented 11 years ago

:+1:

daniel-g commented 10 years ago

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

spork (1.0.0rc4)
spork-rails (4.0.0)
johnnyshields commented 10 years ago

Workaround: use Zeus.

TheNaoX commented 10 years ago

:(

Arcolye commented 10 years ago

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

VeridionRO commented 10 years ago

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