Skip to content

Instantly share code, notes, and snippets.

@Pistos
Last active November 13, 2021 03:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pistos/d2013bde9e482ee0396a3d5822ef0cbf to your computer and use it in GitHub Desktop.
Save Pistos/d2013bde9e482ee0396a3d5822ef0cbf to your computer and use it in GitHub Desktop.
Using Database Cleaner and FactoryBot with Hanami
# spec/factories/account.rb
FactoryBot.define do
factory :account, class: AccountRepository do
# ...
end
end
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
FactoryBot.find_definitions
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
FactoryBot.define do
initialize_with do
new.create(attributes)
end
to_create do |instance|
# Do nothing, because the instance should already have been saved at build
# time by the Hanami repository
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment