How to enable page caching in a functional test in rails?

痞子三分冷 提交于 2019-12-24 00:33:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!