【问题标题】:How to enable page caching in a functional test in rails?如何在 Rails 的功能测试中启用页面缓存?
【发布时间】:2011-02-15 11:31:18
【问题描述】:

是否可以为功能测试打开页面缓存?以下方法无效:

class ArticlesControllerTest < ActionController::TestCase
 def setup
    ActionController::Base.public_class_method :page_cache_path
    ActionController::Base.perform_caching = true
 end
end

提前致谢

德布

【问题讨论】:

    标签: ruby-on-rails functional-testing page-caching


    【解决方案1】:

    我不知道为什么这不起作用,所以我最终直接在 environments/test.rb 上启用缓存:

    config.action_controller.perform_caching             = true
    

    【讨论】:

    • 我正在 Rails 3.1.8 中尝试这个,但它似乎不起作用。每次请求都会重新生成页面。
    【解决方案2】:

    我目前的解决方法是启用perform_caching 然后重新加载控制器:

    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
    

    在最新版本的 Rails 2 中,您遇到的问题与 caches_actioncaches_page 类方法有关。它们都创建了一个环绕过滤器来进行缓存,但仅在启用perform_caching 时。

    因此,在运行时修改 perform_caching 不会重新创建预期的周围过滤器。上面的示例是强制重新评估控制器的一种方法。

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 2011-06-29
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      相关资源
      最近更新 更多