Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:58:32+00:00 2026-06-13T12:58:32+00:00

Looking through a tutorial on controller testing the author gives an example of an

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:58:33+00:00Added an answer on June 13, 2026 at 12:58 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking through a code tutorial just now, and found something interesting --
I was looking through a Storyboard tutorial here: http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1 When I noticed that the
I've been looking through a tutorial and book but I can find no mention
Looking through angular js tutorial , I do not understand where the repeater (function?)
I'm going through a tutorial to learn how to use Backbone.js and I'm having
I've been looking through the tutorial : http://www.vogella.de/articles/AndroidSQLite/article.html and SQLite seems to have all
I've been search and looking through forums and postings for tutorial(s) on, I think
After looking through tutorial on http://oreilly.com/catalog/expjava/excerpt/index.html below is what I have written. I wanted
Hey, I was looking through W3's tutorial on AJAX and I decided to make
Looking through multiple iOS crash logs, I'm finding stack traces which are near identical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.