mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
![Josh Clayton and Jason Draper](/assets/img/avatar_default.png)
Instead of calling before_create, after_build, after_create, or after_stub, you can now call: before(:create) {|instance| instance.name = "overridden!" } after(:create) {|instance| instance.name = "overridden!" } after(:build) {|instance| instance.name = "overridden!" } after(:stub) {|instance| instance.name = "overridden!" } Additionally, you can declare callbacks longhand: callback(:after_stub) {|instance| instance.name = "overridden!" } This allows for custom callbacks to be defined: callback(:custom_callback) {|instance| instance.name = "overridden!" } Which can then be used from a custom strategy: class CustomStrategy def association(runner); end def result(evaluation) evaluation.object.tap do |instance| evaluation.notify(:custom_callback, instance) end end end FactoryGirl.register_strategy(:custom, CustomStrategy) This would allow for calling: FactoryGirl.custom(:user) Which would return the user instance but execute the :custom_callback callback on the user instance first.
62 lines
1.8 KiB
Ruby
62 lines
1.8 KiB
Ruby
require "spec_helper"
|
|
|
|
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
|
|
before do
|
|
define_model("User", email: :string)
|
|
FactoryGirl.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_girl.run_factory") do
|
|
FactoryGirl.build(:slow_user)
|
|
end
|
|
|
|
time_to_execute.should be_within(0.03).of(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]
|
|
tracked_invocations[factory_name] ||= {}
|
|
tracked_invocations[factory_name][strategy_name] ||= 0
|
|
tracked_invocations[factory_name][strategy_name] += 1
|
|
end
|
|
|
|
ActiveSupport::Notifications.subscribed(callback, "factory_girl.run_factory") do
|
|
FactoryGirl.build_list(:slow_user, 2)
|
|
FactoryGirl.build_list(:user, 5)
|
|
FactoryGirl.create_list(:user, 2)
|
|
FactoryGirl.attributes_for(:slow_user)
|
|
end
|
|
|
|
tracked_invocations[:slow_user].should == { build: 2, attributes_for: 1 }
|
|
tracked_invocations[:user].should == { build: 5, create: 2 }
|
|
end
|
|
end
|