FactoryBot::Sorbet
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add factory_bot-sorbet
If bundler is not being used to manage dependencies, install the gem by executing:
gem install factory_bot-sorbet
Usage
This gems adds the module FactoryBot::Sorbet
which contains
methods for using all your factories in a type-safe way.
The problem
Given a factory
FactoryBot.define do
factory :foo, class_name: "Foo" do
bar { "BAR!" }
end
end
Using regular FactoryBot you'd use it like so:
FactoryBot.create(:foo, baz: 1) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=1>
FactoryBot.build(:foo, baz: 2) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=2>
FactoryBot.build_stubbed(:foo, baz: 3) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=3>
This DSL has one problem when used with sorbet. It is impossible to create
a static signature for create
, build
, build_stubbed
because they
return a different type each time based on the first argument.
The solution
This gem defines unique methods for each factory and defines static sorbet signatures for them using a Tapioca compiler.
FactoryBot::Sorbet.foo(:create, baz: 1) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=1>
FactoryBot::Sorbet.foo(:build, baz: 2) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=2>
FactoryBot::Sorbet.foo(:build_stubbed, baz: 3) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=3>
Tapioca will generate the following RBI file to make Sorbet fully understand the types of your factories.
# sorbet/rbi/dsl/factory_bot/sorbet.rbi
# typed: true
# DO NOT EDIT MANUALLY
# This is an autogenerated file for dynamic methods in `FactoryBot::Sorbet`.
# Please instead update this file by running `bin/tapioca dsl FactoryBot::Sorbet`.
module FactoryBot::Sorbet
sig { params(strategy: Symbol, args: T.anything, block: NilClass).returns(Foo) }
sig do
type_parameters(:R)
.params(
strategy: Symbol,
args: T.anything,
block: T.proc.params(arg0: Foo).returns(T.type_parameter(:R))
).returns(T.type_parameter(:R))
end
def foo(strategy, *args, &block); end
end
Test Helpers
If you use Rails/Minitest (or any other test framework) we highly recommend adding a helper methods for easier access to your factories.
# test/test_helper.rb
require 'factory_bot/sorbet'
class Minitest::Test
include FactoryBot::Sorbet::TestHelpers
end
This will the let you use the shorthand:
f.foo(:create)
Instead of:
FactoryBot::Sorbet.foo(:create)
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/Verseth/ruby-factory_bot-sorbet.