Question

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.

Was it helpful?

Solution

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 calling new; however, any other class methods you want to call will have to be called on the class explicitly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top