Hi Dan,
Thanks for taking a look, I know many people, including myself, really
want callbacks in FG.
The goal of callbacks is two-fold:
(1) make your factories more expressive and therefore make your tests
easier to read
(2) treat your models like they would be treated in the application
(1): Currently there is no way to do has_many in factory_girl. Instead
of using `Factory(:user_with_expensive_items)` you must construct the
relations in the setup portion of your test or create a helper to do
it. This puts logic, that you shouldn't need to grok to understand the
test, into the setup block or sticks that logic in yet another
location. I believe callbacks help encapsulate the purpose of the
object graph you're constructing better and thereby simplify your
tests and make them more readable.
We already do this with other attributes giving us `Factory
(:administrative_user)`, `Factory(:delinquent_account)`, etc, but are
currently unable to do this with has_many relationships. Callbacks
give us this. Heck, we can even do this with belongs_to right now. Why
not with has_many?
(2): By adjusting the state (as in state-machine) of your model the
way your application would you arrive at the same state (as in
attributes) your application would, no surprises. This may make your
factories less brittle, allowing you to change what adjusting the
state (as in state machine) does to your model state (as in
attributes) without having to fix factories, but instead fix just
tests.
I'm more certain of the first point, less of the second. The first
point is the entire reason I wrote this extension, the second is one
that I've heard from others.
Thanks for your time and consideration.
Nate aka fowlduck
On Oct 10, 12:37 pm, Dan Croak <
dcr...@thoughtbot.com> wrote:
> Nate,
>
> Your callbacks branch looks good to me. Well-written, well-tested.
> Thank you for taking the time to write it and share it with others.
>
> I know callbacks have been talked about for Factory Girl since it was
> released and there has been resistance to including them. I can't
> remember why. I want to say lack of compelling use cases?
>
> I'll bring this up to the other thoughtbot guys on Monday.
>
> For (mostly my) reference, the cases laid out so far are:
>
> verified_user case:
>
>
http://groups.google.com/group/factory_girl/browse_thread/thread/f649...
>
> has_many while respecting build strategy case:
>
>
http://groups.google.com/group/factory_girl/browse_thread/thread/9059...