Things You May Not Know About FactoryGirl
OK, I have been doing a lot (and I mean, a lot) of work on Rails, recently. I have always loved the simple expressive syntax of Ruby, anyway, so that does not bother me.
Now, in one of the projects, I have been using ActiveAdmin, and other awesome gems like FactoryGirl, RSpec, Cucumber, etc. And, every now and then, I come across a concept or feature about these gems that I was not aware about earlier. I wanted to quickly jot down all these things, in a single place (for future reference). Now, many of these things might not be new to you, but are to me and are of importance for me. I am wondering if I should have used a different title for this post, now.
I knew about this feature (obviously). It is simple: if you need an attribute to change everytime a new factory is built or created – use a sequence.
The above sequence will generate different emails each time the factory is called upon, and thus, helps us in passing validations on unique attributes.
This is really cool. The value of one of the attributes can depend upon the value of another attribute in our factory. Combined with on-the-fly attribute declaration, we have a really powerful way to create/build factories:
1 2 3 4 5
Now, when we build the
user factory, we get a
username and a corresponding
url for our User. But, we can pass a
username: nil option to our factory,
and both our
url fields will be set to
1 2 3 4 5 6
Callbacks and Transient Attributes
We can, even, define some virtual features on our factory. For example, we can
with_<feature> transient attribute for our factory, which decides if
a particular feature will be available in our factory or not. For example:
1 2 3 4 5 6 7 8 9 10 11 12
This will ensure that a
user factory is created with an associated profile
with_profile: true is passed as an option. Otherwise, a profile for that
user will not be created.
Traits define a group of attributes on the concerned factory which form a part of a particular behaviour. Now, I never had much use of this particular feature, but I can see this feature is really really useful.
I won’t go into much details of this feature, as it has been described pretty well in the documentation (yes, I am aware, docs are pretty awesome, and this post might be really useless).