1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
Commit graph

11 commits

Author SHA1 Message Date
Jan Bernacki
09481eebef Allow use of Symbol#to_proc in FactoryGirl callbacks
This allows callbacks to be called as such:

    FactoryGirl.define do
      factory :user do
        after :create, &:confirm!
      end
    end

Closes #511
2013-04-05 10:14:29 -04:00
Joshua Clayton
36cb43e9b0 Add global callbacks
This allows callbacks (after :build, :create, etc.) to be defined at the
FactoryGirl level; this means that the callback will be invoked for all
factories. This is primarily to maintain consistency and follow the
principle of least surprise.

As usual, callbacks are applied from the lowest component to the
highest, meaning that global callbacks will be run after factory and
trait callbacks are run.

    FactoryGirl.define do
      after(:build) {|object| puts "Built #{object}" }

      factory :user
      # ...
    end

Closes #481
Closes #486
2013-02-08 11:24:35 -05:00
Joshua Clayton
b095f24598 Convert to expect syntax 2013-01-18 13:58:36 -05:00
Joshua Clayton
f92195f35a Support binding a block to multiple callbacks
This allows for binding multiple callbacks (after_stub, before_create,
etc.) to a single block. This is useful if you want a block to be called
across all build strategies (since build_stubbed doesn't share any
callbacks with build/create).

Examples:

    factory :user do
      callback(:after_stub, :before_create) { do_something }
      after(:stub, :create) { do_something_else }
      before(:create, :custom) { do_a_third_thing }
    end
2012-09-11 11:14:26 -04:00
Josh Clayton and Jason Draper
bef5a01b31 Introduce new callback syntax
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.
2012-05-04 17:59:34 -04:00
Joshua Clayton
327e0c1f2b Allow implicit syntax methods in callbacks 2012-05-04 10:48:52 -04:00
Joshua Clayton
6c29b11477 Use 1.9 hash syntax 2012-03-16 16:43:23 -04:00
Thomas Walpole
27c4b21e6b make sure parent callbacks are run before child 2011-09-22 09:51:08 -04:00
Joshua Clayton
0b2c4da0a4 Move DefineConstant code into macro 2011-08-19 17:21:54 -04:00
Joe Ferris
6c2322a11d New default syntax for using defined factories 2011-01-26 20:44:24 -05:00
Joe Ferris
2d1c77984b Split up the acceptance spec; removed the acceptance model fixtures in favor of fresh definitions in each spec 2010-11-12 14:58:25 -06:00