You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
recently I start refactoring our test, move large object creation from let to let_it_be, which will change object creation from before_each hook into before_all hook, then I found some tests failed due to eager loading detected, like below
RSpec.describePost,type: :modeldo# post has_many comments# comment belongs_to post# creator belongs_to commentlet_it_be(:post){create(:post)}let_it_be(:comment){create(:comment,post: post)}# before(:all) do # same as let_it_be# puts "start before all"# @post = create(:post)# @comment = create(:comment, post: @post)# puts "end before all"# endit'ok'docreator=create(:creator,comment_id: comment.id)comment.post# okendit'raise N+1 query issue'docreator=create(:creator,comment: comment)comment.post# will raise eager loading Comment => [:post]endend
which will raise error USE eager loading detected, Comment => [:post], Add to your query: .includes([:post]), reproducable repo is here. I don't think the detection is correct :(
I read the source code of bullet and found the issue is the object created in before_all hook didn't add into Bullet::Detector::NPlusOneQuery::impossible_objects, because Bullet.start_request is invoked in before_each hook.
# n_plus_one_query.rbdefadd_impossible_object(object)returnunlessBullet.start?# will return when object creation in `before_all` hookreturnunlessBullet.n_plus_one_query_enable?returnunlessobject.bullet_primary_key_valueBullet.debug('Detector::NPlusOneQuery#add_impossible_object',"object: #{object.bullet_key}")impossible_objects.addobject.bullet_keyend# spec_helper.rb, will start Bullet in before_each hook.ifBullet.enable?RSpec.configuredo |config|
config.beforedoBullet.start_requestendconfig.afterdoBullet.perform_out_of_channel_notificationsifBullet.notification?Bullet.end_requestendendend
so my questions are:
is there a way to avoid eager loading detected error when I use before_all or let_it_be? I really want to move some large object creation out of each test, which will significantly improve performance
no matter the problem is solved or not, I hope the documentation can cover this scenario, it may help other people on it :)
The text was updated successfully, but these errors were encountered:
Hi, zm
recently I start refactoring our test, move large object creation from
let
to let_it_be, which will change object creation frombefore_each
hook intobefore_all
hook, then I found some tests failed due toeager loading detected
, like belowwhich will raise error
USE eager loading detected, Comment => [:post], Add to your query: .includes([:post])
, reproducable repo is here. I don't think the detection is correct :(I read the source code of bullet and found the issue is the object created in
before_all
hook didn't add intoBullet::Detector::NPlusOneQuery::impossible_objects
, because Bullet.start_request is invoked inbefore_each
hook.so my questions are:
eager loading detected
error when I usebefore_all
orlet_it_be
? I really want to move some large object creation out of each test, which will significantly improve performanceThe text was updated successfully, but these errors were encountered: