爲了演示的目的,假設我有一個名爲DemoThing
的類,其方法名爲do_something
。ActiveRecord:檢查數據庫調用次數
有沒有辦法(代碼中)我可以檢查do_something
到達數據庫的次數?有沒有一種方法可以在活動記錄上「窺探」來計算數據庫被調用的次數?
例如:
class DemoThing
def do_something
retVal = []
5.times do |i|
retVal << MyActiveRecordModel.where(:id => i)
end
retVal
end
end
dt = DemoThing.new
stuff = dt.do_something # want to assert that do_something hit the database 5 times
http://guides.rubyonrails.org/active_support_instrumentation.html – dbugger