Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning for attributes_for in FactoryGirl and Rspec Testing

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

like image 929
thank_you Avatar asked Oct 31 '12 02:10

thank_you


People also ask

What is an RSpec test?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

Is RSpec used for unit testing?

RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool.


1 Answers

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

like image 68
pjam Avatar answered Sep 18 '22 09:09

pjam