O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mutual Attributes and Having

You can also create several entities together with a mutual list of attributes. Let’s say we want to create a few users, all sharing the same country_id attribute. We could use the having macro, like this:

 ~~~
 Forge.having country_id: usa_id do
  user = Forge.user
  admin = Forge.admin
 end
 ~~~

Now, both user and admin will have a country_id equal to usa_id. This advantage is small here, but will grow significantly with the number of attributes and entities that you need to manage. For example, in our tests, we may have dozens of questions and choices, all which have the same survey_id and company_id.

What if we need to create some other type of entity, like, say, an Elixir struct?

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required