【问题标题】:Rails disable database to test cachingRails 禁用数据库以测试缓存
【发布时间】:2014-09-30 02:32:52
【问题描述】:

我想断言一些东西,以确保它来自缓存并且没有命中数据库。我试过这个:

assert_equal 29, Article.sum # assume this is DB-intensive
Article.clear_all_connections!
assert_equal 29, Article.sum # cached result

但是连接会自动重新建立,所以clear_all_connections! 在这里没有任何作用。如何切断与数据库的联系?并暂时这样做,以便稍后在测试中恢复。

【问题讨论】:

    标签: ruby-on-rails caching testing activerecord ruby-on-rails-4


    【解决方案1】:

    一个更简单和更通用的解决方案原来是counting the number of queries。为了确保一切都来自缓存,只需在某些操作期间断言计数器为零。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 2013-05-04
      • 2011-07-11
      • 1970-01-01
      • 2011-10-02
      相关资源
      最近更新 更多