Optionally testing caching in Rails 3 functional tests

A solution for rspec:

Add an around block with a custom metadata key to your configuration.

RSpec.configure do |config|
  config.around(:each, :caching) do |example|
    caching = ActionController::Base.perform_caching
    ActionController::Base.perform_caching = example.metadata[:caching]
    example.run
    Rails.cache.clear
    ActionController::Base.perform_caching = caching
  end
end

Add the metadata key when caching is needed.

describe "visit the homepage", :caching => true do
  # test cached stuff
end

You're liable to end up with tests stomping on each other. You should wrap this up with ensure and reset it to old values appropriately. An example:

module ActionController::Testing::Caching
  def with_caching(on = true)
    caching = ActionController::Base.perform_caching
    ActionController::Base.perform_caching = on
    yield
  ensure
    ActionController::Base.perform_caching = caching
  end

  def without_caching(&block)
    with_caching(false, &block)
  end
end

I've also put this into a module so you can just include this in your test class or parent class.