【发布时间】:2015-06-30 04:31:32
【问题描述】:
我想测试我的缓存行为,但类似的事情失败了:
context "without cache" do
it "should return the appropriate result, computing it every time", :caching => false do
p ActionController::Base.perform_caching
Object.should_receive(:where).exactly(4).and_call_original
result1 = SomeService.action(args1, args2)
result2 = SomeService.action(args1, args2)
end
end
...即使为ActionController::Base.perform_caching 打印出false。 [这被集团的:caching => false 选项禁用(我使用钩子)]
如果我通过从默认的:memory_store 切换到测试环境的:null_store 来完全关闭缓存,则测试会按预期通过。我只是在test.rb 中添加这一行:
config.cache_store = :null_store
我的 action 方法使用 Rails 的核心缓存:
def action(args1, args2):
...
objects = Rails.cache.fetch(cache_key, expires_in: 5.days) do
object.where(id: args1).where(charac: args2).includes(...).to_a
end
...
end
【问题讨论】:
-
我试图通过最后一次编辑让我的问题更容易理解,我希望这很有用:-)
标签: ruby-on-rails caching