# Accessing named route helper in cache sweepers

**URL:** <https://www.ruby-forum.com/t/accessing-named-route-helper-in-cache-sweepers/177866>\
**Category:** Rails\
**Created:** [October 25, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/accessing-named-route-helper-in-cache-sweepers/177866 "2009-10-25T23:16:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottj97](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@scottj97](https://www.ruby-forum.com/u/scottj97)\
**Post date:** [October 25, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/accessing-named-route-helper-in-cache-sweepers/177866/1 "2009-10-25T23:16:55Z")

</div>

In my cache sweeper I call expire\_fragment and use some named route  
helpers for its argument (like comments\_path(…)).

This works fine in the app, but in my tests it’s causing problems,  
because there’s no controller and comments\_path returns nil. (See  
method\_missing in actionpack/lib/action\_controller/caching/sweeper.rb  
– that’s what’s returning nil in this case. I don’t understand why  
that’s acceptable, but whatever.)

When my tests run Comment.create() directly, the sweeper gets called,  
with no controller. How can I get to comments\_path in this case?

---

<div class="post-metadata">

**Author:** ![scottj97](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@scottj97](https://www.ruby-forum.com/u/scottj97)\
**Post date:** [October 26, 2009, 3:23am UTC](https://www.ruby-forum.com/t/accessing-named-route-helper-in-cache-sweepers/177866/2 "2009-10-26T03:23:55Z")

</div>

On Sun, Oct 25, 2009 at 3:15 PM, Scott J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> When my tests run Comment.create() directly, the sweeper gets called,  
> with no controller. How can I get to comments\_path in this case?

I do know if the most effective way to do things because caching is  
turned  
off by default within the test configuration. Next, where’s the code  
for  
the  
test(s) with issues?

-Conrad

---

<div class="post-metadata">

**Author:** ![scottj97](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@scottj97](https://www.ruby-forum.com/u/scottj97)\
**Post date:** [October 26, 2009, 5:35am UTC](https://www.ruby-forum.com/t/accessing-named-route-helper-in-cache-sweepers/177866/3 "2009-10-26T05:35:01Z")

</div>

The test is trivial. Using rspec and machinist:

In user\_spec.rb:

describe User do  
it “should create a new instance given valid attributes” do  
User.create!(User.plan)  
end  
end

And in blueprints.rb:

User.blueprint do  
login { Sham.name }  
email { “#{self.login}@foo.com” }  
password { Sham.name }  
password\_confirmation { password}  
end

I just realized that this works when run by itself. It only breaks  
when run after other specs that use controllers with the cache\_sweeper  
directive. (I guess that causes the sweeper to get loaded and begin  
observing the models.)
