strict_loading_by_default
was added to ActiveRecord 6.1. How can we eager load fixture associations to make tests pass once this flag is used?
class ApplicationRecord < ActiveRecord::Base
self.strict_loading_by_default = true
end
# app/models/post.rb
class Post < ApplicationRecord
belongs_to :author
end
# app/models/author.rb
class Author < ApplicationRecord; end
# test/fixtures/posts.yml
my_post:
author: my_author
# test/fixtures/authors.yml
my_author: {}
# test/models/post_test.rb
class PostTest < ActiveSupport::TestCase
test "post belongs to author" do
post = posts(:my_post)
assert post.author # Minitest::UnexpectedError: ActiveRecord::StrictLoadingViolationError:
# `Author` called on `Post` is marked for strict_loading and cannot be lazily loaded.
end
end