Created
October 28, 2024 14:24
-
-
Save kaspth/ef8a2a8b7966f1e570db0f30b99e607e to your computer and use it in GitHub Desktop.
Using https://github.com/kaspth/oaken like factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using Oaken like factories | |
# | |
# 1. only include `db:seed` seeds in development: | |
# db/seeds/development/**/* | |
# db/seeds.rb | |
Oaken.prepare do | |
# 2. Then setup any common defaults | |
defaults name: -> { Faker::Name.name } | |
accounts.defaults name: -> { Faker::Business.name } | |
# 3. Or add helper methods in case you need more advanced setup | |
# Maybe a `factory` method could be a good fit! | |
# Keep it simple: you probably don't want to interact with more than 3 models, this is not a place for FactoryBot like traits | |
def accounts.factory(**) = create(**) | |
end | |
# test/models/account_test.rb | |
class AccountTest < ActiveSupport::TestCase | |
setup do | |
# 4. Now in your tests, you have no loaded seeds, but can use your factories and start tieing things together | |
@account = accounts.factory(…) | |
@user = user.factory(account: @account) | |
end | |
test "something" do | |
account = accounts.factory(…) | |
end | |
end | |
# You could later extract any definitely shared test setup into the test only directory: | |
# db/seeds/test/**/* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment