module Cosinux module PageCacheTest def self.included(base) #:nodoc: ActionController::Base.public_class_method :page_cache_path ActionController::Base.perform_caching = true end # asserts that the list of given url are being cached # - it first makes sure each url is not cached, # - then if a block is given, it yields it, otherwise it executes a get request on each url, # - and finally it asserts that the url are cached. def assert_cache(*urls) silence do urls.each do |url| ActionController::Base.expire_page(url) end end if block_given? yield *urls else urls.each { |url| get url } end urls.each do |url| assert_block("#{url.inspect} is not cached after executing block") do File.exists? page_cache_path(url) end end end # asserts that the list of given url are being expired # - it first makes sure each url is cached, # - then it yields the block # - and finally it asserts that the urls are expired def assert_expire(*urls) silence do urls.each do |url| ActionController::Base.cache_page("testing", url) end end yield *urls urls.each do |url| assert_block("#{url.inspect} is cached after executing block") do ! File.exists? page_cache_path(url) end end end private def page_cache_path(url) ActionController::Base.page_cache_path(url) end def silence logger = ActionController::Base.logger old_logger_level, logger.level = logger.level, Logger::ERROR if logger yield ensure logger.level = old_logger_level if logger end end end