Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 1.43 KB

File metadata and controls

35 lines (28 loc) · 1.43 KB

ActiveRecordFixtures

Tapioca::Dsl::Compilers::ActiveRecordFixtures decorates RBIs for test fixture methods that are created dynamically by Rails.

For example, given an application with a posts table, we can have a fixture file

first_post:
  author: John
  title: My post

Rails will allow us to invoke posts(:first_post) in tests to get the fixture record. The generated RBI by this compiler will produce the following

# test_case.rbi
# typed: true
class ActiveSupport::TestCase
  include ActiveRecord::TestFixtures

  sig { returns(T::Array[Post]) }                                                          #   No names: returns an Array of all fixtures
  sig { params(fixture_name: T.any(String, Symbol)).returns(Post) }                        #   One name: returns the requested fixture
  sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)) # Many names: returns an Array of the requested fixtures
  .returns(T::Array[Post]) }
  def posts(fixture_name = nil, *other_fixtures); end
end

The include is generated because Rails mixes ActiveRecord::TestFixtures into ActiveSupport::TestCase through the :active_support_test_case load hook in rails/test_help.rb. Since RBI generation does not load an app's test helper, this runtime include is not captured. Without it, Sorbet does not see the class methods the module contributes via mixes_in_class_methods, such as fixtures.