2010-05-04 34 views
2

是否可以打开功能测试的页面缓存?以下没有工作:如何在rails的功能测试中启用页面缓存?

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

在此先感谢

德布

回答

0

我无法弄清楚,为什么这是行不通的,所以我结束了直接environments/test.rb启用缓存:

config.action_controller.perform_caching    = true 
+0

我想在Rails 3.1.8中使用它,它似乎不工作。每次请求都会重新生成页面。 – 2012-10-09 09:25:56

3

我目前的解决方法是启用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不会重新创建期望的过滤器。上面的例子是强制你的控制器重新评估的一种方法。

+1

适合我,谢谢。 – fguillen 2011-08-08 10:17:11