fabrication-gem

How do you DRY up subclassed fabricators?


So I have a parent class with a fabricator, and two subclasses. How do I get the subclasses to reference the parent class's fabricator for setting up shared code?

E. G.

Fabricator(:parent) do
    important_variable "Foo"
    lesser_variable "Bar
end

Fabricator(:child1) do
    //Not sure I actually need anything in here
end


Fabricator(:child2) do
    //Again, not sure I actually need anything in here
end

Fabricate(:child).important_variable #Foo
Fabricate(:child).lesser_variable #Bar

Solution

  • You can pass a from parameter to the children like so:

    Fabricator(:child1, from: :parent) do
      //Not sure I actually need anything in here
    end
    
    
    Fabricator(:child2, from: :parent) do
      //Again, not sure I actually need anything in here
    end
    

    You can read more about it in the fabrication docs.

    https://www.fabricationgem.org/#defining-fabricators