I want to reference the class represented by the factory during factory creation. Is that possible?
Here's what I'm doing now:
factory :foo do # Foo class
name "Foo"
...
initialize_with { Foo.some_complicated_method(attributes) }
factory :bar_foo do
name "Bar"
initialize_with { BarFoo.some_complicated_method(attributes) }
end
end
What I'd like to do is reference the class name is such a way that I can do this (replacing __factory_class__
with the appropriate method call):
factory :foo do
name "Foo"
...
initialize_with { __factory_class__.some_complicated_method(attributes) }
factory :bar_foo do # Runs initialize_with using BarFoo instead of Foo
name "Bar"
end
end
Is what I'm wanting possible? Note that BarFoo
is a subclass of Foo
.
It's not possible. It's described in FactoryGirl's Getting Started docs in Custom Construction section:
When using
initialize_with
, you don't have to declare the class itself when callingnew
; however, any other class methods you want to call will have to be called on the class explicitly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With