Testing fragment creation and expire fragment in Rails (3.0.7)

495 Views Asked by At

I'm attempting to implement fragment caching in my application. It displays a lot of data that rarely changes so I figured that fragment caching would be the best solution for some performance problems I've been having.

I'm using Rail's built-in fragment caching and cache sweepers, but I can't seem to find any good way of testing the creation/expiration of the fragments (using rspec and friends).

Any help would be much appreciated.


Clarification: All I really want to test is that the appropriate fragments are expired, not that anything in particular is going in or coming out of the cache.

1

There are 1 best solutions below

0
On

With mocha it would be something like this:

UserController.any_instance.expects(:expire_fragment).with('cache_key')

The only thing we care about with this test is that the method actually gets called with the appropriate arguments not if the method actually does the job.

Used in rspec:

it 'expires the awesome user cache' do
  UserController.any_instance.expects(:expire_fragment).with('awesome_user_cache').once

  post :create, id: user.id, ect...
end