Meaning for attributes_for in FactoryGirl and Rspec Testing
attributes_for
will return a hash, whereas build
will return a non persisted object.
Given the following factory:
FactoryGirl.define do factory :user do name 'John Doe' endend
Here is the result of build
:
FactoryGirl.build :user=> #<User id: nil, name: "John Doe", created_at: nil, updated_at: nil>
and the result of attributes_for
FactoryGirl.attributes_for :user=> {:name=>"John Doe"}
I find attributes_for
very helpful for my functional test, as I can do things like the following to create a user:
post :create, user: FactoryGirl.attributes_for(:user)
When using build
, we would have to manually create a hash of attributes from the user
instance and pass it to the post
method, such as:
u = FactoryGirl.build :userpost :create, user: u.attributes # This is actually different as it includes all the attributes, in that case updated_at & created_at
I usually use build
& create
when I directly want objects and not an attributes hash
Let me know if you need more details