问题
Is it possible to turn on page caching for a functional test? The following didn't work:
class ArticlesControllerTest < ActionController::TestCase
def setup
ActionController::Base.public_class_method :page_cache_path
ActionController::Base.perform_caching = true
end
end
thanks in advance
Deb
回答1:
My current workaround is to enable perform_caching
then reload the controller:
class ProjectsCachingTest < ActionController::IntegrationTest
def setup
# force the controller to be reloaded when caching is enabled
ActionController::Base.perform_caching = true
load "projects_controller.rb"
end
def teardown
# undo the actions above
ActionController::Base.perform_caching = false
load "projects_controller.rb"
end
end
In the latest version of Rails 2, the issue you are experiencing has to do with the class methods caches_action
and caches_page
. They both create an around filter to do the caching, but only when perform_caching
is enabled.
So, modifying perform_caching
at runtime does not recreate the expected around filters. The example above is one way to force your controller to be re-evaluated.
回答2:
I couldn't figure out why this was not working, so I ended up enabling caching directly on environments/test.rb
:
config.action_controller.perform_caching = true
来源:https://stackoverflow.com/questions/2765700/how-to-enable-page-caching-in-a-functional-test-in-rails