Skip to content

Commit d457ab8

Browse files
mokevninclaude
authored andcommitted
Address review: correct the source of the include, keep it for unmapped fixtures
Two fixes from review. The include does not come from a railtie initializer — there is none. It comes from the `:active_support_test_case` load hook in `rails/test_help.rb`, which an app requires from `test_helper.rb`, so it never runs during RBI generation. Reword the comment and the manual accordingly. The early return sat below the `select!` that drops fixture sets whose model constant cannot be resolved, so an app with fixture files but no matching models wrote `fixtures :all` and still got no include. Move the return above the `select!`: apps with no fixture files at all keep generating nothing, everything else gets the include. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent f20146c commit d457ab8

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

lib/tapioca/dsl/compilers/active_record_fixtures.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ module Compilers
3737
# ~~~
3838
#
3939
# The `include` is generated because Rails mixes `ActiveRecord::TestFixtures` into
40-
# `ActiveSupport::TestCase` from the `active_record.test_fixtures` railtie initializer, which
41-
# does not run during gem RBI generation. Without it, Sorbet does not see the class methods
42-
# the module contributes via `mixes_in_class_methods`, such as `fixtures`.
40+
# `ActiveSupport::TestCase` from a `:active_support_test_case` load hook in `rails/test_help.rb`,
41+
# which is only required from an app's `test_helper.rb` and therefore never runs during RBI
42+
# generation. Without it, Sorbet does not see the class methods the module contributes via
43+
# `mixes_in_class_methods`, such as `fixtures`.
4344
#: [ConstantType = singleton(ActiveSupport::TestCase)]
4445
class ActiveRecordFixtures < Compiler
4546
MISSING = Object.new
@@ -53,9 +54,10 @@ def decorate
5354
method_names_from_eager_fixture_loader
5455
end
5556

56-
method_names.select! { |name| fixture_class_mapping_from_fixture_files[name] != MISSING }
5757
return if method_names.empty?
5858

59+
method_names.select! { |name| fixture_class_mapping_from_fixture_files[name] != MISSING }
60+
5961
root.create_path(constant) do |mod|
6062
mod.create_include("ActiveRecord::TestFixtures")
6163

manual/compiler_activerecordfixtures.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ end
2929
~~~
3030

3131
The `include` is generated because Rails mixes `ActiveRecord::TestFixtures` into
32-
`ActiveSupport::TestCase` from the `active_record.test_fixtures` railtie initializer, which
33-
does not run during gem RBI generation. Without it, Sorbet does not see the class methods
34-
the module contributes via `mixes_in_class_methods`, such as `fixtures`.
32+
`ActiveSupport::TestCase` from a `:active_support_test_case` load hook in `rails/test_help.rb`,
33+
which is only required from an app's `test_helper.rb` and therefore never runs during RBI
34+
generation. Without it, Sorbet does not see the class methods the module contributes via
35+
`mixes_in_class_methods`, such as `fixtures`.

spec/tapioca/dsl/compilers/active_record_fixtures_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ def posts(fixture_name = nil, *other_fixtures); end
9191
assert_equal(expected, rbi_for("ActiveSupport::TestCase"))
9292
end
9393

94+
it "generates only the include if no fixture has an associated model" do
95+
add_content_file("test/fixtures/serialized_data.yml", <<~YAML)
96+
---
97+
field1: 123
98+
name: Hello
99+
YAML
100+
101+
expected = <<~RBI
102+
# typed: strong
103+
104+
class ActiveSupport::TestCase
105+
include ActiveRecord::TestFixtures
106+
end
107+
RBI
108+
109+
assert_equal(expected, rbi_for("ActiveSupport::TestCase"))
110+
end
111+
94112
it "generates methods for fixtures" do
95113
add_content_file("test/fixtures/posts.yml", <<~YAML)
96114
super_post:

0 commit comments

Comments
 (0)