FactoryGirl: why does attributes_for omit some attributes?
Asked Answered
B

5

16

I want to use FactoryGirl.attributes_for in controller testing, as in:

it "raise error creating a new PremiseGroup for this user" do
  expect {
    post :create, {:premise_group => FactoryGirl.attributes_for(:premise_group)}
  }.to raise_error(CanCan::AccessDenied)
end

... but this doesn't work because #attributes_for omits the :user_id attribute. Here is the difference between #create and #attributes_for:

>> FactoryGirl.create(:premise_group)
=> #<PremiseGroup id: 3, name: "PremiseGroup_4", user_id: 6, is_visible: false, is_open: false)
>> FactoryGirl.attributes_for(:premise_group)
=> {:name=>"PremiseGroup_5", :is_visible=>false, :is_open=>false}

Note that the :user_id is absent from #attributes_for. Is this the expected behavior?

FWIW, my factories file includes definitions for :premise_group and for :user:

FactoryGirl.define do
  ...
  factory :premise_group do
    sequence(:name) {|n| "PremiseGroup_#{n}"}
    user
    is_visible false
    is_open false
  end
  factory :user do
    ...
  end
end
Bond answered 24/4, 2012 at 0:42 Comment(0)
B
26

Short Answer:

By design, FactoryGirl's attribues_for intentionally omits things that would trigger a database transaction so tests will run fast. But you can can write a build_attributes method (below) to model all the attributes, if you're willing to take the time hit.

Original answer

Digging deep into the FactoryGirl documentation, e.g. this wiki page, you will find mentions that attributes_for ignores associations -- see update below. As a workaround, I've wrapped a helper method around FactoryGirl.build(...).attributes that strips id, created_at, and updated_at:

def build_attributes(*args)
  FactoryGirl.build(*args).attributes.delete_if do |k, v| 
    ["id", "created_at", "updated_at"].member?(k)
  end
end

So now:

>> build_attributes(:premise_group)
=> {"name"=>"PremiseGroup_21", "user_id"=>29, "is_visible"=>false, "is_open"=>false}

... which is exactly what's expected.

update

Having absorbed the comments from the creators of FactoryGirl, I understand why attributes_for ignores associations: referencing an association generates a call to the db which can greatly slow down tests in some cases. But if you need associations, the build_attributes approach shown above should work.

Bond answered 24/4, 2012 at 8:26 Comment(3)
Nevermind. Looks like there's a few such issues: github.com/thoughtbot/factory_girl/issues/316Cutin
I have currently put the build_attributes method under spec/spec_helper.rb. Or should I be putting it elsewhere?Titoism
@MikeHan: Putting #build_attributes in spec/spec_helper.rb should be fine. In my case, I put it in spec/factories.rb file. It might be slightly cleaner to put it in spec/factory_girl_helper.rb and include it when you need it.Bond
C
1

I think this is a slight improvement over fearless_fool's answer, although it depends on your desired result.

Easiest to explain with an example. Say you have lat and long attributes in your model. On your form, you don't have lat and long fields, but rather lat degree, lat minute, lat second, etc. These later can converted to the decimal lat long form.

Say your factory is like so:

factory :something
  lat_d 12
  lat_m 32
  ..
  long_d 23
  long_m 23.2
end

fearless's build_attributes would return { lat: nil, long: nil}. While the build_attributes below will return { lat_d: 12, lat_m: 32..., lat: nil...}

def build_attributes
  ba = FactoryGirl.build(*args).attributes.delete_if do |k, v| 
    ["id", "created_at", "updated_at"].member?(k)
  end
  af = FactoryGirl.attributes_for(*args)
  ba.symbolize_keys.merge(af)
end
Cassatt answered 21/12, 2012 at 5:40 Comment(0)
O
0

To further elaborate on the given build_attributes solution, I modified it to only add the accessible associations:

def build_attributes(*args)
    obj = FactoryGirl.build(*args)
    associations = obj.class.reflect_on_all_associations(:belongs_to).map { |a| "#{a.name}_id" }
    accessible = obj.class.accessible_attributes

    accessible_associations = obj.attributes.delete_if do |k, v| 
        !associations.member?(k) or !accessible.include?(k)
    end

    FactoryGirl.attributes_for(*args).merge(accessible_associations.symbolize_keys)
end
Offshoot answered 15/9, 2013 at 15:4 Comment(1)
I haven't tried your code, but I suspect you mean delete_if !member? AND !include? -- is that correct?Bond
B
0

Here is another way:

FactoryGirl.build(:car).attributes.except('id', 'created_at', 'updated_at').symbolize_keys

Limitations:

  • It does not generate attributes for HMT and HABTM associations (as these associations are stored in a join table, not an actual attribute).
  • Association strategy in the factory must be create, as in association :user, strategy: :create. This strategy can make your factory very slow if you don't use it wisely.
Baillieu answered 3/7, 2017 at 20:32 Comment(0)
H
0

The accepted answer seems outdated as it did not work for me, after digging through the web & especially this Github issue, I present you:

A clean version for the most basic functionality for Rails 5+

This creates :belongs_to associations and adds their id (and type if :polymorphic) to the attributes. It also includes the code through FactoryBot::Syntax::Methods instead of an own module limited to controllers.

spec/support/factory_bot_macros.rb

module FactoryBot::Syntax::Methods
  def nested_attributes_for(*args)
    attributes = attributes_for(*args)
    klass = args.first.to_s.camelize.constantize

    klass.reflect_on_all_associations(:belongs_to).each do |r|
      association = FactoryBot.create(r.class_name.underscore)
      attributes["#{r.name}_id"] = association.id
      attributes["#{r.name}_type"] = association.class.name if r.options[:polymorphic]
    end

    attributes
  end
end

this is an adapted version of jamesst20 on the github issue - kudos to him 👏

Hassi answered 9/7, 2019 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.