Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

url_helpers not available in sweepers in test environment

#4868

I think this behavior most likely predates Rails 3, but I'm just starting to zero in on it now.

I've noticed that the url_helpers are available in my sweepers (ActionController::Caching::Sweeper subclasses) in my development and production environments, but not in my test environment.

For example, in the production/development environments this kind of sweeper works:

class ProductSweeper < ActionController::Caching::Sweeper
  observe Product

  def after_save product
    expire_page(product_path(product) + '.html')
  end
end

The same call fails in the test environment because product_path() always returns nil.

It would appear that the cause is that the url_helpers haven't been included in the sweeper in that environment, and so the "method_missing" handling in actionpack/lib/action_controller/caching/sweeping.rb kicks in and returns nil. (And obviously, expire_page(nil + '.html') doesn't do anything helpful.)

If I explicitly include the helpers by adding this to the sweeper:

include Rails.application.routes.url_helpers

Then the sweeper works in the test environment.

I'm currently trying to identify why it is that the helpers are included in all environments except the test environment. If anybody knows why, please comment. Seems like a bug because it's surprising at best, and inconsistent at worst.

Reported by Wincent Colaiuta · June 15th, 2010 @ 09:21 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh

    It seems you already know the solution. Can you provide a code patch and a test?

    June 26th, 2010 @ 05:16 AM

  2. Wincent Colaiuta
    Wincent Colaiuta
    • Importance changed from to Low

    Well, I don't actually know the solution, I only know a workaround: ie. explicitly including the url helpers in each sweeper (this effectively means that in the development and production environments they get included twice, but it seems harmless).

    I've tried to trace through the code paths in the different environments to work out why the helpers are included in one two environments and not the others, but I haven't yet been able to figure it out. I haven't given up trying though, just haven't had time to get to the bottom of it yet.

    As for the test, yes, I can probably get that done even if I can't figure out the real cause.

    June 26th, 2010 @ 10:49 AM

  3. Andrew White
    Andrew White

    I'm pretty sure the reason is that if you've a big routes.rb file you probably don't want them loaded in every test case. That way you can include them in the ones that matter or define a subset of routes in the test.

    June 26th, 2010 @ 12:13 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 05:01 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:01 PM