Meaning for attributes_for in FactoryGirl and Rspec Testing

thank_you picture thank_you · Oct 31, 2012 · Viewed 20.2k times · Source

Looking through a tutorial on controller testing the author gives an example of an rspec test testing a controller action. My question is, why did they use the method attributes_for over build? There is no clear explanation why attributes_for is used besides that it returns a hash of values.

it "redirects to the home page upon save" do
  post :create, contact: Factory.attributes_for(:contact)
  response.should redirect_to root_url
end

The tutorial link is found here: http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html The example is found at the beginning topic section Controller testing basics

Answer

pjam picture pjam · Oct 31, 2012

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'
  end
end

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 :user
post :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