Created
March 28, 2016 16:36
-
-
Save bignimbus/015d03abaa22fddc7196 to your computer and use it in GitHub Desktop.
Using FactoryGirl callbacks
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
# 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