Skip to content

Instantly share code, notes, and snippets.

@itarato
Last active April 8, 2024 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itarato/bb4fcc882350d15df9f5dbae1aa3de91 to your computer and use it in GitHub Desktop.
Save itarato/bb4fcc882350d15df9f5dbae1aa3de91 to your computer and use it in GitHub Desktop.
Use Sorbet compatible setup-initializations in tests

Use Sorbet compatible setup-initializations in tests

Sorbet can help with type inference in tests if the setup is right. Otherwise types will be reduced to T.untyped.

Don't do

class Test < ActiveSupport::TestCase
  def setup # Don't use `dev setup`.
    @user = make_fixture(User, :alex) # Don't forget `T.let`
  end
end

Do

class Test < ActiveSupport::TestCase
  setup do
    @user = T.let(make_fixture(User, :alex), User)
  end
end

With this latter @user will be infered as type User, and it does not need to be nilable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment