From af19f8ca1f09007d6dbdef94536bae702686f737 Mon Sep 17 00:00:00 2001 From: Thong Kuah Date: Sun, 18 Aug 2019 08:57:24 +0000 Subject: Require request specs when testing N+1 for controllers --- doc/development/query_recorder.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/development/query_recorder.md b/doc/development/query_recorder.md index a6b60149ea4..4fbf289b596 100644 --- a/doc/development/query_recorder.md +++ b/doc/development/query_recorder.md @@ -36,6 +36,13 @@ it "avoids N+1 database queries" do end ``` +## Use request specs instead of controller specs + +Use a [request spec](https://relishapp.com/rspec/rspec-rails/v/3-8/docs/request-specs/request-spec) when writing a N+1 test on the controller level. + +Controller specs should not be used to write N+1 tests as the controller is only initialized once per example. +This could lead to false successes where subsequent "requests" could have queries reduced (e.g. because of memoization). + ## Finding the source of the query It may be useful to identify the source of the queries by looking at the call backtrace. -- cgit v1.2.1