[factory_girl] Factory Girl with non-AR models

340 views
Skip to first unread message

Bradley

unread,
Apr 28, 2010, 9:48:29 AM4/28/10
to factory_girl
I'm trying to stub out some service models that I have and I'd like to
use factory_girl as that's what I'm using for my AR models also. Is
this possible?

I just tried creating the class (in lib) and factory below to no
avail. I get wrong # of arguments. I'm not sure how factory_girl
reflects on that object to determine what params to pass in. Do I
need to define my stub class in some specific way?

class SomeStub

attr_accessor :name, :age

def initialize(name, age)
@name=name
@age=age
end
end

with Factory:

Factory.define :some_stub, :default_strategy => :build do |s|
s.name 'some stub'
s.age 456
end

--
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

Michael Breen

unread,
Apr 28, 2010, 12:50:27 PM4/28/10
to factor...@googlegroups.com
Why wouldn't you just use mocha, rr, or some other mocking lib?

Best.
Mike

Bradley

unread,
May 4, 2010, 3:06:47 PM5/4/10
to factory_girl
ya i've never used mocha, just thought i'd see if it was possible to
stick with one framework for this type of thing, i'll look into it i
guess if it's not possible with factory_girl
> For more options, visit this group athttp://groups.google.com/group/factory_girl?hl=en

Keenan Brock

unread,
May 4, 2010, 4:26:38 PM5/4/10
to factor...@googlegroups.com
While I agree with Mike's comment.

The error is because of your constructor

Make it more ActiveRecord like

def initialize(*args={})
args.each_pair do |n,v|
self.send("#{n}=",v)
end
end

See how that works,
Keenan

Michael Breen

unread,
May 4, 2010, 6:51:43 PM5/4/10
to factor...@googlegroups.com
If you're going to go that route you won't want to use the splat operator with a default since that'll blow up on ya ;)

def initialize(args={})
args.each_pair do |n,v|
self.send("#{n}=",v)
end
end

Best.
Mike
Reply all
Reply to author
Forward
0 new messages