Initialization Problems

3 views
Skip to first unread message

Michael Koziarski

unread,
Oct 24, 2008, 7:34:54 AM10/24/08
to factory_girl
Hey guys,

Firstly, this is cool stuff. I've just replaced all my custom
valid_user_attributes methods with factory calls, and it's working
great. Except for one thing.

Requiring the library causes it to attempt to load the factories:

cattr_accessor :definition_file_paths
self.definition_file_paths = %w(factories test/factories spec/
factories)

Which means if I have, config.gem set, and reference a constant in my
factories like this:

Factory.define :confirmed_user, :class=>User do |u|
...
end

The user file will be required before the plugins have loaded, so my
has_attachment and friends will implode.

Any ideas on the right fix here?

Josh Nichols

unread,
Oct 24, 2008, 9:22:21 AM10/24/08
to factor...@googlegroups.com
Quickest fix is probably to fork factory_girl, and remove that from
factory_girl.rb.

Because of that problem, I'd probably recommend removing it in
general, and instead suggest people do Factory.find_definitions in
their test_helper or spec_helper.

Joe.R....@gmail.com

unread,
Oct 24, 2008, 9:55:27 AM10/24/08
to factory_girl
Hey,

I'm planning on fixing this in factory_girl by loading factory
definitions after the Rails environment has loaded, but for now, you
can try wrapping your factory definitions like this:

Rails.configuration.after_initialize do

# Factory definitions here

end

That should work until I can get a working fix released.

Thanks for giving factory_girl a try!

-Joe

Michael Koziarski

unread,
Oct 24, 2008, 10:18:52 AM10/24/08
to factory_girl
> Rails.configuration.after_initialize do
>
>   # Factory definitions here
>
> end
>
> That should work until I can get a working fix released.

I just removed the config.gem and added the gem + require call to my
factories.rb. Worked a charm, but a little less 'cool' :)

> Thanks for giving factory_girl a try!

I've been pretty impressed thus far, there are a few features which
would probably be nice. Things like "Take a random existing X", and
slightly nicer support for building nested has_many associations.
What kind of features, if any, do you have in mind for future
releases.

But on the whole, this is way nicer than my home grown methods like
'new_user' and 'valid_user_attributes'.

Josh Nichols

unread,
Oct 24, 2008, 1:15:38 PM10/24/08
to factor...@googlegroups.com
I was actually just working on this. Mostly trying to get rid of the
need for my factory_girl_on_rails plugin.

I've pushed it up to github:
http://github.com/technicalpickles/factory_girl/tree/rails

It does 2 things (and I realize I probably should have done them separately).

First,

* If Rails is defined...
* Use the Rails.configuration
* Add set the default factory paths to stuff inside of RAILS_ROOT
* Then do the Factory.find_definitions
* Otherwise, just do Factory.find_definitions

Second,

* Supports loading factories from like test/factories.rb,
test/factories/post_factory.rb, etc.

Reply all
Reply to author
Forward
0 new messages