Got already a Factory model

3 views
Skip to first unread message

Cédric Brancourt

unread,
Oct 28, 2009, 10:28:08 AM10/28/09
to factory_girl
I use factory_girl to fake some model for some cucumber steps (In a
Rails app).
But I already have a Factory model (oops ...)
So What is the more convenient way to deal with this ?

Ryan Angilly

unread,
Oct 28, 2009, 10:40:31 AM10/28/09
to factor...@googlegroups.com
Not tested, and very hacky, but you can probably require factory_girl before your Rails environment in test_helper.rb and do this:

require 'factory_girl'
Sweatshop = Factory
Object.send :remove_const, 'Factory'

# everything else


And then else where... :

Sweatshop.define :factory do |f|
  f.name "whatever"
end

Cédric Brancourt

unread,
Oct 28, 2009, 11:13:27 AM10/28/09
to factory_girl
Hum , this does not seem to work as intended.
Since I need to go forward, I'll put factory_girl on the side until I
find a more convenient workaround.



On 28 oct, 15:40, Ryan Angilly <angi...@gmail.com> wrote:
> Not tested, and very hacky, but you can probably require factory_girl before
> your Rails environment in test_helper.rb and do this:
>
> require 'factory_girl'
> Sweatshop = Factory
> Object.send :remove_const, 'Factory'
>
> # everything else
>
> And then else where... :
>
> Sweatshop.define :factory do |f|
>   f.name "whatever"
> end
>
> On Wed, Oct 28, 2009 at 10:28 AM, Cédric Brancourt <
>

Ryan Angilly

unread,
Oct 28, 2009, 11:17:50 AM10/28/09
to factor...@googlegroups.com
The ruby nerd in me wonders why.  Can you elaborate?

Cédric Brancourt

unread,
Oct 28, 2009, 12:12:36 PM10/28/09
to factory_girl
I'll have a look deeper at home to understand why this does not work
as expected.
I guess I have to require factory_girl and rename the constant Factory
in FactoryGirl before the rails models are loaded, and in the
"Cucumber World."

The test_helper was not the good place to do it. (unless i missed
something)



On 28 oct, 16:17, Ryan Angilly <angi...@gmail.com> wrote:
> The ruby nerd in me wonders why.  Can you elaborate?
>
> On Wed, Oct 28, 2009 at 11:13 AM, Cédric Brancourt <
>

Mark Wilden

unread,
Oct 28, 2009, 12:26:35 PM10/28/09
to factor...@googlegroups.com
On Wed, Oct 28, 2009 at 9:12 AM, Cédric Brancourt
<cedric.b...@gmail.com> wrote:
>>
>> > > require 'factory_girl'
>> > > Sweatshop = Factory
>> > > Object.send :remove_const, 'Factory'
>>
>> > > # everything else
>>
>> > > And then else where... :
>>
>> > > Sweatshop.define :factory do |f|
>> > >   f.name "whatever"
>> > > end

Judging from the following code, this should work. The only possible
gotcha is that the factory_girl class will still have the name
Factory.

class Factory
def self.say_me
'FactoryGirl'
end
end
factory_girl = Factory.new

FactoryGirl = Factory
Object.send(:remove_const, :Factory)

class Factory
def self.say_me
'Factory'
end
end
factory = Factory.new

puts factory_girl.class.object_id
=> 83340
puts factory.class.object_id
=> 83270

puts factory_girl.class
=> Factory
puts factory.class
=> Factory

puts FactoryGirl.say_me
=> FactoryGirl
puts Factory.say_me
=> Factory

puts factory_girl.class.say_me
=> FactoryGirl
puts factory.class.say_me
=> Factory

///ark

Reply all
Reply to author
Forward
0 new messages