Hooks

In the process of constructing an object from a fixture, it can be necessary (or at least helpful) to run arbitrary code with the constructed object before it is returned: generating passwords, defining relationships, populating a cache, etc. This process is called hookingopen in new window on Wikipedia; factory_botopen in new window calls them "callbacksopen in new window".

In simplest terms, a hook is an object that holds string name and a 2-argument function. When a Strategy calls runHooks("X", instance), all of the hooks that are named "X" are gathered (following the scoping rules) and then, in the order they're defined, their functions are executed on the current instance.

Defaults

By default, there are 3 hooks:

  • after("build", ...) is called after the fixture instance has been built (in fr.build and fr.create)
  • before("create", ...) is called before the fixture instance is saved (in fr.create)
  • after("create", ...) is called after the fixture instance is saved (in fr.create)