Skip to content

Instantly share code, notes, and snippets.

@nirebu
Created July 15, 2022 13:09
Show Gist options
  • Save nirebu/c0660caf052194d749ec90c6b51a9f5c to your computer and use it in GitHub Desktop.
Save nirebu/c0660caf052194d749ec90c6b51a9f5c to your computer and use it in GitHub Desktop.
Enable conditional factory bot monitoring in RSpec
if ENV['MONITOR_FACTORIES']
factory_results = {
time_spent_in_factories: 0,
payloads: [],
}
config.before do
ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |_name, start, finish, _id, payload|
took = finish - start
factory_results[:payloads] << payload
factory_results[:time_spent_in_factories] += took
puts "FactoryBot: #{payload[:strategy]}(:#{payload[:name]}) - took #{took}s"
end
end
config.after do
factory_results[:factories_run] = factory_results.delete(:payloads).map{ [_1[:strategy], _1[:name]] }.tally
pp factory_results
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment