Created
July 15, 2022 13:09
-
-
Save nirebu/c0660caf052194d749ec90c6b51a9f5c to your computer and use it in GitHub Desktop.
Enable conditional factory bot monitoring in RSpec
This file contains hidden or 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
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