Have you ever found yourself creating an ActiveRecord
object with
FactoryBot with the sole purpose
of turning it into a hash of attributes?
> FactoryBot.build(:book).attributes
{ "id"=>nil, "title"=>"Fledgling", "genre"=>"fiction" }
FactoryBot has a built-in method for doing this:
> FactoryBot.attributes_for(:book)
{ title: "Fledgling", genre: "fiction" }
It also accepts any traits for that factory:
> FactoryBot.attributes_for(:book, :published)
{
title: "Fledgling",
genre: "fiction",
publication_year: 2005,
page_count: 362
}
This is a handy way of build a base set of attributes when testing an API endpoint.