How can I check Rails record caching in unit tests? -
i'm trying make unit test ensure operations / not query database. there way can watch queries, or counter can check @ worst?
if intent discern whether or not rails (activerecord) caches queries, don't have write unit test - already exist , part of rails itself.
edit:
in case, see if adapt 1 of strategies rails team uses test activerecord itself. check following test link above:
def test_middleware_caches mw = activerecord::querycache.new lambda { |env| task.find 1 task.find 1 assert_equal 1, activerecord::base.connection.query_cache.length } mw.call({}) end
you may able following:
def check_number_of_queries mw = activerecord::querycache.new lambda { |env| # assuming object set perform operations myobject.first.do_something_and_perform_side_operations puts activerecord::base.connection.query_cache.length.to_s } end
i haven't tried such thing, might worth investigating further. if above return number of cached queries waiting happen, should trivial change puts assert test case.
Comments
Post a Comment