2012-05-03 00:36:58 -04:00
|
|
|
unless ActiveSupport::Notifications.respond_to?(:subscribed)
|
|
|
|
module SubscribedBehavior
|
2018-10-05 14:54:08 -04:00
|
|
|
def subscribed(callback, *args)
|
2012-05-03 00:36:58 -04:00
|
|
|
subscriber = subscribe(*args, &callback)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
unsubscribe(subscriber)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.extend SubscribedBehavior
|
|
|
|
end
|
|
|
|
|
2012-04-24 01:03:28 -04:00
|
|
|
describe "using ActiveSupport::Instrumentation to track factory interaction" do
|
2017-10-20 15:20:28 -04:00
|
|
|
let(:slow_user_factory) { FactoryBot.factory_by_name("slow_user") }
|
|
|
|
let(:user_factory) { FactoryBot.factory_by_name("user") }
|
2012-04-24 01:03:28 -04:00
|
|
|
before do
|
|
|
|
define_model("User", email: :string)
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-04-24 01:03:28 -04:00
|
|
|
factory :user do
|
2018-07-29 11:30:02 -04:00
|
|
|
email { "john@example.com" }
|
2012-04-24 01:03:28 -04:00
|
|
|
|
|
|
|
factory :slow_user do
|
2012-05-04 16:48:46 -04:00
|
|
|
after(:build) { Kernel.sleep(0.1) }
|
2012-04-24 01:03:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "tracks proper time of creating the record" do
|
|
|
|
time_to_execute = 0
|
2018-10-05 14:54:08 -04:00
|
|
|
callback = ->(_name, start, finish, _id, _payload) { time_to_execute = finish - start }
|
2017-10-20 15:20:28 -04:00
|
|
|
ActiveSupport::Notifications.subscribed(callback, "factory_bot.run_factory") do
|
|
|
|
FactoryBot.build(:slow_user)
|
2012-04-24 01:03:28 -04:00
|
|
|
end
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(time_to_execute).to be >= 0.1
|
2012-04-24 01:03:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "builds the correct payload" do
|
|
|
|
tracked_invocations = {}
|
|
|
|
|
2018-10-05 14:54:08 -04:00
|
|
|
callback = ->(_name, _start, _finish, _id, payload) do
|
2012-04-24 01:03:28 -04:00
|
|
|
factory_name = payload[:name]
|
|
|
|
strategy_name = payload[:strategy]
|
2015-05-19 23:18:34 -04:00
|
|
|
factory = payload[:factory]
|
2012-04-24 01:03:28 -04:00
|
|
|
tracked_invocations[factory_name] ||= {}
|
|
|
|
tracked_invocations[factory_name][strategy_name] ||= 0
|
|
|
|
tracked_invocations[factory_name][strategy_name] += 1
|
2015-05-19 23:18:34 -04:00
|
|
|
tracked_invocations[factory_name][:factory] = factory
|
2012-04-24 01:03:28 -04:00
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
ActiveSupport::Notifications.subscribed(callback, "factory_bot.run_factory") do
|
|
|
|
FactoryBot.build_list(:slow_user, 2)
|
|
|
|
FactoryBot.build_list(:user, 5)
|
|
|
|
FactoryBot.create_list(:user, 2)
|
|
|
|
FactoryBot.attributes_for(:slow_user)
|
2012-04-24 01:03:28 -04:00
|
|
|
end
|
|
|
|
|
2015-05-19 23:18:34 -04:00
|
|
|
expect(tracked_invocations[:slow_user][:build]).to eq(2)
|
|
|
|
expect(tracked_invocations[:slow_user][:attributes_for]).to eq(1)
|
|
|
|
expect(tracked_invocations[:slow_user][:factory]).to eq(slow_user_factory)
|
|
|
|
expect(tracked_invocations[:user][:build]).to eq(5)
|
|
|
|
expect(tracked_invocations[:user][:factory]).to eq(user_factory)
|
2012-04-24 01:03:28 -04:00
|
|
|
end
|
|
|
|
end
|