1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/acceptance/activesupport_instrumentation_spec.rb

68 lines
2.1 KiB
Ruby
Raw Normal View History

unless ActiveSupport::Notifications.respond_to?(:subscribed)
module SubscribedBehavior
def subscribed(callback, *args, &block)
subscriber = subscribe(*args, &callback)
yield
ensure
unsubscribe(subscriber)
end
end
ActiveSupport::Notifications.extend SubscribedBehavior
end
describe "using ActiveSupport::Instrumentation to track factory interaction" do
let(:slow_user_factory) { FactoryBot.factory_by_name("slow_user") }
let(:user_factory) { FactoryBot.factory_by_name("user") }
before do
define_model("User", email: :string)
FactoryBot.define do
factory :user do
email "john@example.com"
factory :slow_user do
after(:build) { Kernel.sleep(0.1) }
end
end
end
end
it "tracks proper time of creating the record" do
time_to_execute = 0
callback = ->(name, start, finish, id, payload) { time_to_execute = finish - start }
ActiveSupport::Notifications.subscribed(callback, "factory_bot.run_factory") do
FactoryBot.build(:slow_user)
end
2013-01-18 13:27:57 -05:00
expect(time_to_execute).to be >= 0.1
end
it "builds the correct payload" do
tracked_invocations = {}
callback = ->(name, start, finish, id, payload) do
factory_name = payload[:name]
strategy_name = payload[:strategy]
factory = payload[:factory]
tracked_invocations[factory_name] ||= {}
tracked_invocations[factory_name][strategy_name] ||= 0
tracked_invocations[factory_name][strategy_name] += 1
tracked_invocations[factory_name][:factory] = factory
end
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)
end
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)
end
end