Model "Factory" and factory_girl

Hi!

Another problem…

I am using a model named “Factory” since the beginning of this
project. Now I wanted to migrate my tests that depends on a manually
seeded database over to factory_girl.

In production I can still enter console or start the server, but when
trying to do anything in test-env, then I get the following
stacktrace:

/home/nmelzer/Dokumente/sources/ruby/1.9.2/rails3.1/webworld/app/models/factory.rb:1:in
<top (required)>': Factory is not a class (TypeError) from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:inrequire’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in
block in require' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:225:inload_dependency’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in
require' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:348:inrequire_or_load’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:302:in
depend_on' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:214:inrequire_dependency’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/engine.rb:416:in
block (2 levels) in eager_load!' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/engine.rb:415:ineach’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/engine.rb:415:in
block in eager_load!' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/engine.rb:413:ineach’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/engine.rb:413:in
eager_load!' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/application/finisher.rb:51:inblock in module:Finisher
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/initializable.rb:25:in
instance_exec' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/initializable.rb:25:inrun’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/initializable.rb:50:in
block in run_initializers' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/initializable.rb:49:ineach’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/initializable.rb:49:in
run_initializers' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/application.rb:92:ininitialize!’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/railtie/configurable.rb:30:in
method_missing' from /home/nmelzer/Dokumente/sources/ruby/1.9.2/rails3.1/webworld/config/environment.rb:5:in<top (required)>’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/application.rb:78:in
require' from /home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/application.rb:78:inrequire_environment!’
from
/home/nmelzer/.rvm/gems/ruby-1.9.2-p180@webworld/gems/railties-3.1.0/lib/rails/commands.rb:39:in
<top (required)>' from script/rails:6:inrequire’
from script/rails:6:in `’

This looks plausible to me. Since factory_girl provides a module
Factory.

But how can I avoid this problem, without completely rewriting all
code that depends on my model?

Or alternatively, are there any similar things like Factory_girl that
will not have that collision with my own code?

TIA
Norbert

2011/11/18 Norbert M. [email protected]:

But how can I avoid this problem, without completely rewriting all
code that depends on my model?

I posted this on the github site also, there I was advised to use
::Factory inside my code whenever I am refering my own model. Since
this is a problem that occurs only in testenv, but I have to change my
overall code, I dislike that solution, what brings me to…

Or alternatively, are there any similar things like Factory_girl that
will not have that collision with my own code?

… another gem I stumbled upon yesterday, “Fabrication”
(http://fabricationgem.org/). This does mostly the same as
factory_girl does, and adds some spice. It is capable of generating
cucumbersteps which makes creation of objects in my features a lot
easier, also I can use properties of the fabricated model inside of
the fabricator without to build them in a variable first (a thing I
missed in factory girl)

While I had to do following in FG

Factory.define :user do |user|
user_name = Factory.next(:name) # Just imagine I had used
inlinesequence at this point :slight_smile:
user.name “#{user_name}”
user.email “#{user_name}@example.com
user.password “secret”
end

I can do the following in Fabricatiion:

Fabricator(:user) do
name { sequence { |n| “User#{n}” } }
email { |user|
“#{user.name.downcase.parameterize}@example.com” }
password “secret”
end

This is from a privat project, where I am converting my Factorys to
Fabricators, also I converted all sequences to inline where possible
(this is something I had done with FG soon too)

I hope this one helps others too!
Bye
Norbert