Mass-assign empty strings in FactoryBot
I recently had to write some RSpec specs for an importer job that pulled in claims
data to a table that had 600 columns. That was fine until we found out that all of those columns had to be set to null: false
. Now in order to write a spec for a single one of these objects, we had to have some non-null value in all 600 attributes. Yikes.
Fortunately, "" != nil
. This means we could mass assign all attributes to an empty string (""
) and the null constraints were satisfied. This is how to mass-assign ""
to an object with the Rails gem, FactoryBot:
factory :claims do
# This table has null constrains on all (~600) columns.
# This block mass-assigns an empty string to each attribute:
initialize_with do
new(Claims.new.attributes.symbolize_keys.transform_values(&:to_s))
end
end
Now, in your spec, you can treat this factory as you normally would, overriding the default ""
values as needed:
let(:claims) { create(:claims, name: 'sunshine', location: 'all the dark corners of the world') }