2010-05-04 33 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