how to use factory in controller specs of nested resource?

176 views
Skip to first unread message

Gordon

unread,
Mar 26, 2012, 8:41:03 AM3/26/12
to factory_girl
hi all,

how do you use factory girl in controller specs of a nested
resource?

I am running rails 3.2.0.
I have the following nested resource controller which is failing.
I know very well that the nested resource's object needs the id of the
parent object to search for. The factory girl object for the parent
object, @category is valid as i can print its id out but when the
nested resource's controller tries to use the category id value from
the parameters (as assigned to in valid_attributes) ,it fails saying
that the category id is not there.
I'm puzzled.

Consider the controller spec below:

-------------- extract of spec/controllers/
sub_categories_controller_spec.rb - start ----------
describe SubCategoriesController do

# This should return the minimal set of attributes required to
create a valid
# SubCategory. As you add validations to SubCategory, be sure to
# update the return value of this method accordingly.

@category = FactoryGirl.create(:category_intakes)

def valid_attributes
{
:name => 'turbos',
:description => 'Mr Wankel should be proud',
:category => @category.id, # or even, "@category.id.to_s,"
:created_by => 1,
:updated_by => 1
}
end

login_admin_user


describe "GET index" do
it "assigns all sub_categories as @sub_categories" do
sub_category = SubCategory.create! valid_attributes
get :index
assigns(:sub_categories).should eq([sub_category])
end
end

-------------- extract of spec/controllers/
sub_categories_controller_spec.rb - end ----------

-------- extract of controllers/sub_categories_controller.rb --- start
--------

class SubCategoriesController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
before_filter :find_category

# GET /sub_categories
# GET /sub_categories.json
def index
@sub_categories = @category.sub_categories
puts " In the index of sub cats now ..." +
@sub_categories.inspect

respond_to do |format|
format.html # index.html.erb
format.json { render :json => @sub_categories }
end
end

private
def find_category
@category = Category.find(params[:category_id])
end
end

-------- extract of controllers/sub_categories_controller.rb --- end
--------

The error message I see with the spec's failure reads, "

1) SubCategoriesController GET new assigns a new sub_category as
@sub_category
Failure/Error: get :new
ActiveRecord::RecordNotFound:
Couldn't find Category without an ID
# /Users/anexiole/projects/try_rails/app/controllers/
sub_categories_controller.rb:102:in `find_category'
# ./sub_categories_controller_spec.rb:59:in `block (3 levels) in
<top (required)>'
"

How do we use factory girl with rspec controller of nested resources?

Keenan Brock

unread,
Apr 3, 2012, 9:58:09 AM4/3/12
to factor...@googlegroups.com
Hello Gordon,

a) I think you want:

setup do
  @category = FactoryGirl.create(:category_intakes)
end

b) as for valid_attributes, you may want to look into FactoryGirl.attributes_for. It may provide a few shortcuts for you
FactoryGirl.attribtues_for(:sub_category, :category_id => @category.id)

c) I'm thinking category => @category.id should probably read category_id => @category.id

d) I worry about sending created_by and updated_by to the controller.
That seems like a security vulnerability of your app.
You should probably protect those attributes and have the controller set them.


Thinking about it, it seems that 

Just a few thought.

Best of luck,
Keenan
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:

You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group at

Keenan Brock

unread,
Apr 3, 2012, 10:15:55 AM4/3/12
to factor...@googlegroups.com
Gordon,

Oops, I didn't finish that last sentence, I added another post, with a code example that works for me (it is a real project I'm working on), but I don't like my solution.

—Keenan
Reply all
Reply to author
Forward
0 new messages