Avoiding `from: :class` when defining fabricators

16 views
Skip to first unread message

Pablo Olmos de Aguilera Corradini

unread,
Oct 11, 2014, 6:24:57 PM10/11/14
to fabrica...@googlegroups.com
I'm using fabrication with rails, and I normally have more than one fabricator for each class/model. I found myself, creating many

Fabricator :somethng, from: :class_name

I found this repetitive, because it's obvious that in that file, every fabricator should inherit the class, for instance

# spec/fabricator/disease_fabricator.rb

Fabricator :disease do; end
Fabricator :asthma, from: :disease do
  name        
"Asthma"
  shortname  
"asthma"
  description
"Asthma is a common chronic inflammatory disease of the airways characterized by variable and recurring symptoms, reversible airflow obstruction and bronchospasm"
  icpc2      
"R96"
end
Fabricator :copd, from: :disease do
  name        
"chronic obstructive plmonary dz"
  shorname    
"copd"
  description
"blablaba"
  icpc2      
"R93"
end

I remember that factory girls, allows somehing like:

Factory :disease do
 
Factory :asthma do
    name        
"Asthma"
    shortname  
"asthma"
    description
"Asthma is a common chronic inflammatory disease of the airways characterized by variable and recurring symptoms, reversible airflow obstruction and bronchospasm"
    icpc2      
"R96"
 
end
 
Factory :copd do
    name        
"chronic obstructive plmonary dz"
    shorname    
"copd"
    description
"blablaba"
    icpc2      
"R93"
 
end
end

(I'm not quite sure about the syntax, but it was something like that).

Anyway, if I'm using rails, and is running disease_fabricator, it makes sense that by default it should be assumed that every Fabricator defined belongs to that class.

Is this behaviour possible?

Paul Elliott

unread,
Oct 11, 2014, 6:35:11 PM10/11/14
to fabrica...@googlegroups.com
Hey Pablo,

There is no way to do that in the current version, although it could be added to the class lookup.

I would typically not have very specific fabricators like you have though. I would make a general one using ffaker for dummy data, then override specific fields as necessary in specs. They have a medical module if you need it to sort of look like the real thing.

Unless I the content of a field is important to the spec, it should be random data.

-- Paul
--
You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabricationge...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Paul
Reply all
Reply to author
Forward
0 new messages