Skip to content

Instantly share code, notes, and snippets.

@bignimbus
Created March 28, 2016 16:36
Show Gist options
  • Save bignimbus/015d03abaa22fddc7196 to your computer and use it in GitHub Desktop.
Save bignimbus/015d03abaa22fddc7196 to your computer and use it in GitHub Desktop.
Using FactoryGirl callbacks
# let's say I want to execute a callback before making a FactoryGirl fixture using #create
factory :thing do
before(:create) do |foo|
foo.bar = create :baz
end
end
# but then I also want to call the same block after making a FactoryGirl fixture using #build
factory :thing do
before(:create) do |foo|
foo.bar = create :baz
end
after(:build) do |foo|
foo.bar = create :baz
end
end
# that's not DRY, but using FactoryGirl#callback is:
factory :thing do
callback(:before_create, :after_build) do |foo|
foo.bar = create :baz
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment