This project is archived and is in readonly mode.

#4868 ✓stale
Wincent Colaiuta

url_helpers not available in sweepers in test environment

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

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')

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.

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh June 26th, 2010 @ 05:16 AM

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

  • Wincent Colaiuta

    Wincent Colaiuta June 26th, 2010 @ 10:49 AM

    • 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.

  • Andrew White

    Andrew White June 26th, 2010 @ 12:13 PM

    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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>