This is my Tag model and I don't know how can I test Rails.cache feature.
class Tag < ActiveRecord::Base
class << self
def all_cached
Rails.cache.fetch("tags.all", :expires_in => 3.hours) do
Tag.order('name asc').to_a
end
end
def find_cached(id)
Rails.cache.fetch("tags/#{id}", :expires_in => 3.hours) do
Tag.find(id)
end
end
end
attr_accessible :name
has_friendly_id :name, :use_slug => true, :approximate_ascii => true
has_many :taggings #, :dependent => :destroy
has_many :projects, :through => :taggings
end
Do you know how can it will be tested ?
Well, first, you shouldn't really be testing the framework. Rails' caching tests ostensibly cover that for you. That said, see this answer for a little helper you can use. Your tests would then look something like:
This doesn't actually check the caching mechanism - just that the
#fetch
block isn't invoked. It's brittle and tied to the implementation of the fetch block, so beware that as it will become maintenance debt.