Problem using factory_girl with cucumber

295 views
Skip to first unread message

cnk

unread,
Dec 4, 2009, 11:26:00 PM12/4/09
to factory_girl
I saw the earlier thread on this list and it worked fine - for
cucumber 0.3.11

http://groups.google.com/group/factory_girl/browse_thread/thread/d6c1f8704c17de79/58db623915b35615?lnk=gst&q=uninitialized+constant+Factory#58db623915b35615

But I am trying to move my code to a new machine and am having trouble
getting factory girl recognized by Cucumber. I have tried (separately)
adding config.gem "factory_girl" and the "require 'rubygems'; require
'factory_girl' lines from the post above to my RAILS_ROOT/config/
environments/cucumber.rb file. Neither of them gets rid of the
uninitialized constant Factory (NameError) error.

Putting "require 'factory_girl'" into features/support/env.rb does get
rid of the NameError - and now I have errors telling my that it does
not recognize my factory. I suspect that is because I am using a mix
of syntaxes - Shoulda/TestUnit for my unit and functional tests but
rspec for my cucumber features and cucumber is probably looking for
factories in spec/factories. My factories are in the directory "test/
factories" and are picked up just fine on my current host (despite the
mix of TestUnit and Rspec), but not on my new one.

Old
host New host

cucumber
(0.3.11) cucumber (0.4.3)
thoughtbot-factory_girl (1.2.1)
factory_girl (1.2.3)
rspec (1.2.7, 1.2.6, 1.1.12, 1.1.11)
rspec (1.2.9)
rspec-rails
(1.2.7.1) rspec-rails (1.2.9)
ruby-enterprise-1.8.6-20080709
ruby-enterprise-1.8.7-2009.10

Satyajit Malugu

unread,
Dec 23, 2009, 12:39:48 AM12/23/09
to factory_girl
Hi,

I am getting the same exact error, can any help?

Thanks,
Satyajit

On Dec 4, 8:26 pm, cnk <cynthia.ki...@gmail.com> wrote:
> I saw the earlier thread on this list and it worked fine - for
> cucumber 0.3.11
>

> http://groups.google.com/group/factory_girl/browse_thread/thread/d6c1...

Paul Hinze

unread,
Dec 24, 2009, 1:50:57 PM12/24/09
to factor...@googlegroups.com
Satyajit Malugu <malugu....@gmail.com> on 2009-12-23 at 07:43:

> On Dec 4, 8:26�pm, cnk <cynthia.ki...@gmail.com> wrote:
> > I saw the earlier thread on this list and it worked fine - for
> > cucumber 0.3.11
> >
> > http://groups.google.com/group/factory_girl/browse_thread/thread/d6c1...
> >
> > But I am trying to move my code to a new machine and am having trouble
> > getting factory girl recognized by Cucumber. I have tried (separately)
> > adding config.gem "factory_girl" and the "require 'rubygems'; require
> > 'factory_girl' lines from the post above to my RAILS_ROOT/config/
> > environments/cucumber.rb file. Neither of them gets rid of the
> > uninitialized constant Factory (NameError) error.
> >
> > Putting "require 'factory_girl'" into features/support/env.rb does get
> > rid of the NameError - and now I have errors telling my that it does
> > not recognize my factory. I suspect that is because I am using a mix
> > of syntaxes - Shoulda/TestUnit for my unit and functional tests but
> > rspec for my cucumber features and cucumber is probably looking for
> > factories in spec/factories. My factories are in the directory "test/
> > factories" and are picked up just fine on my current host (despite the
> > mix of TestUnit and Rspec), but not on my new one.
>
> I am getting the same exact error, can any help?

Here is the relevant code that you're fighing with:

http://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl.rb

Line 20 defines the Kernel#Factory method:

def Factory (name, attrs = {})
Factory.default_strategy(name, attrs)
end

This makes it confusing to me that when you add the require to the
environemnts/cucumber.rb that Factory is still undefined. Are you sure
that the cucumber environment is loaded, and that your env.rb isn't just
setting `ENV['RAILS_ENV'] = 'test'` (as it did in older versions)? You
can make sure that config/environemnts/cucumber.rb is being evaluated by
throwing in something like `raise "hey i got here"` somewhere in the
file.

As for your second strategy, of doing `require 'factory_girl'` in your
env.rb, you'll see from line 24 on:

if defined? Rails.configuration
Rails.configuration.after_initialize do
Factory.definition_file_paths = [
File.join(RAILS_ROOT, 'test', 'factories'),
File.join(RAILS_ROOT, 'spec', 'factories')
]
Factory.find_definitions
end
else
Factory.find_definitions
end

So here you've got the logic that factory girl uses. If rails has been
initialized, (i.e. if you have loaded config/environment.rb sometime _before_
this code is running), then it will automatically look in test/factories and
spec/factories and wait to load them after rails is done. Otherwise it calls
find_definitions immediately using the default set of definition_file_paths,
which you'll see from line 26 here

http://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/factory.rb

Is initialized to this:

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

Hopefully this gives you a little more understanding of what is going on during
factory girl init and will help you solve your issues. To summarize I'd say
make sure you're using the right environment file, and also make sure you
initialize rails before you require factory_girl.

Cheers,

Paul

Satyajit Malugu

unread,
Dec 24, 2009, 5:23:42 PM12/24/09
to factor...@googlegroups.com
Thanks Paul for the detailed explanation. For me the problem was resolved after I created config/environments/cucumber.rb. 
(I found this solution based on some kind person's solution in the same mailing list)
I remember in older version this file was created/present now after I added this file and did a require of factory_girl in that file and everything ran correctly.


--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
http://thoughtbot.com/services/training

You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
factory_girl...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/factory_girl?hl=en



--
Satyajit

adamski

unread,
Jan 22, 2011, 10:43:10 AM1/22/11
to Satyajit Malugu, factor...@googlegroups.com
Hello

I am coming up against this issue also - since getting Spork and RSpec
2 running nicely, going back to my Cucumber features I am now getting
the error:

uninitialized constant Factory

All was working fine a few days ago, before I got Spork and RSpec
working together. I also use Factories in some of my RSpec tests and
no problems there.

I wonder if any of the tips in this post would affect Cucumber and
Factory Girl?: https://groups.google.com/group/rubyonrails-talk/browse_thread/thread/130595e259935b4b?hl=en

If anyone has any advice on this please let me know!

Thank you


On Dec 24 2009, 10:23 pm, Satyajit Malugu <malugu.satya...@gmail.com>
wrote:


> Thanks Paul for the detailed explanation. For me the problem was resolved
> after I created config/environments/cucumber.rb.
> (I found this solution based on some kind person's solution in the same
> mailing list)
> I remember in older version this file was created/present now after I added
> this file and did a require of factory_girl in that file and everything ran
> correctly.
>
>
>
>
>

> On Thu, Dec 24, 2009 at 10:50 AM, Paul Hinze <paul.t.hi...@gmail.com> wrote:
> > Satyajit Malugu <malugu.satya...@gmail.com> on 2009-12-23 at 07:43:


> > > On Dec 4, 8:26 pm, cnk <cynthia.ki...@gmail.com> wrote:
> > > > I saw the earlier thread on this list and it worked fine - for
> > > > cucumber 0.3.11
>
> > > >http://groups.google.com/group/factory_girl/browse_thread/thread/d6c1.
> > ..
>
> > > > But I am trying to move my code to a new machine and am having trouble
> > > > getting factory girl recognized by Cucumber. I have tried (separately)
> > > > adding config.gem "factory_girl" and the "require 'rubygems'; require
> > > > 'factory_girl' lines from the post above to my RAILS_ROOT/config/
> > > > environments/cucumber.rb file. Neither of them gets rid of the
> > > > uninitialized constant Factory (NameError) error.
>
> > > > Putting "require 'factory_girl'" into features/support/env.rb does get
> > > > rid of the NameError - and now I have errors telling my that it does
> > > > not recognize my factory. I suspect that is because I am using a mix
> > > > of syntaxes - Shoulda/TestUnit for my unit and functional tests but
> > > > rspec for my cucumber features and cucumber is probably looking for
> > > > factories in spec/factories. My factories are in the directory "test/
> > > > factories" and are picked up just fine on my current host (despite the
> > > > mix of TestUnit and Rspec), but not on my new one.
>
> > > I am getting the same exact error, can any help?
>
> > Here is the relevant code that you're fighing with:
>

> >http://github.com/thoughtbot/factory_girl/blob/master/lib/factory_gir...

> >http://github.com/thoughtbot/factory_girl/blob/master/lib/factory_gir...


>
> > Is initialized to this:
>
> >  self.definition_file_paths = %w(factories test/factories spec/factories)
>
> > Hopefully this gives you a little more understanding of what is going on
> > during
> > factory girl init and will help you solve your issues.  To summarize I'd
> > say
> > make sure you're using the right environment file, and also make sure you
> > initialize rails before you require factory_girl.
>
> > Cheers,
>
> > Paul
>
> > --
> > Individuals over processes. Interactions over tools. Agile Rails training
> > from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
> >http://thoughtbot.com/services/training
>
> > You received this message because you are subscribed to the "factory_girl"
> > mailing list.
> > To post to this group, send email to factor...@googlegroups.com
> > To unsubscribe from this group, send email to

> > factory_girl...@googlegroups.com<factory_girl%2Bunsubscribe@google groups.com>

Reply all
Reply to author
Forward
0 new messages