Gordon
2012-Jan-23 10:35 UTC
[rspec-users] Nested resource view spec seems to be referring to a non-existent route
Hi all, In a view spec for a nested resource, do I need to instantiate/stub the parent resource before I stub the nested resource? I am asking this because all my view specs are failing for a new nested resource I have introduced in my application. The nested resource works as expected when I manually test it though :( Here''s how my edit view spec looks like. ----------- "./spec/views/sub_categories/edit.html.erb_spec.rb" - start -------- require ''spec_helper'' describe "sub_categories/edit.html.erb" do before(:each) do @sub_category = assign(:sub_category, stub_model(SubCategory, :name => ''International interest rates'', :description => ''Comprehensive rates covering Australia, NZ, Malaysia and Singapore'', :category_id => 3, :created_by => 1, :updated_by => 1 )) end it "renders the edit sub category form" do render # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "form", :action => category_sub_categories(@sub_category), :method => "post" do assert_select "input#sub_category_name", :name => "sub_category[name]" assert_select "textarea#sub_category_description", :name => "sub_category[description]" end end end ----------- "./spec/views/sub_categories/edit.html.erb_spec.rb" - end -------- Here''s an extract of the failure: ----------- extract start ------------------------- 1) sub_categories/edit.html.erb renders the edit sub category form Failure/Error: render ActionView::Template::Error: undefined method `sub_category_path'' for #<#<Class: 0x0000010127d2b8>:0x000001016e2380> # ./app/views/sub_categories/_form.html.erb:1:in `_app_views_sub_categories__form_html_erb__4092631658606598204_2155519360'' # ./app/views/sub_categories/edit.html.erb:3:in `_app_views_sub_categories_edit_html_erb___3853358586184509671_2155544160'' # ./spec/views/sub_categories/edit.html.erb_spec.rb:15:in `block (2 levels) in <top (required)>'' ----------- extract end ------------------------- Here''s what my form partial looks like ----- app/views/sub_categories/_form.html.erb start --------------------- <%= form_for [@category, @sub_category] do |f| %> <% if @sub_category.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@sub_category.errors.count, "error") %> prohibited this sub_category from being saved:</h2> <ul> <% @sub_category.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :description %><br /> <%= f.text_area :description %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> ----- app/views/sub_categories/_form.html.erb end --------------------- Here''s what I see when I run ''rake routes'': ------- routes start --------------------------- category_sub_categories GET /categories/:category_id/ sub_categories(.:format) {:action=>"index", :controller=>"sub_categories"} POST /categories/:category_id/ sub_categories(.:format) {:action=>"create", :controller=>"sub_categories"} new_category_sub_category GET /categories/:category_id/ sub_categories/new(.:format) {:action=>"new", :controller=>"sub_categories"} edit_category_sub_category GET /categories/:category_id/ sub_categories/:id/edit(.:format) {:action=>"edit", :controller=>"sub_categories"} category_sub_category GET /categories/:category_id/ sub_categories/:id(.:format) {:action=>"show", :controller=>"sub_categories"} PUT /categories/:category_id/ sub_categories/:id(.:format) {:action=>"update", :controller=>"sub_categories"} DELETE /categories/:category_id/ sub_categories/:id(.:format) {:action=>"destroy", :controller=>"sub_categories"} categories GET / categories(.:format) {:action=>"index", :controller=>"categories"} POST / categories(.:format) {:action=>"create", :controller=>"categories"} new_category GET /categories/ new(.:format) {:action=>"new", :controller=>"categories"} edit_category GET /categories/:id/ edit(.:format) {:action=>"edit", :controller=>"categories"} category GET / categories/:id(.:format) {:action=>"show", :controller=>"categories"} PUT / categories/:id(.:format) {:action=>"update", :controller=>"categories"} DELETE / categories/:id(.:format) {:action=>"destroy", :controller=>"categories"} root / ------- routes end --------------------------- The form partial has been properly fitted with the parent resource and the nested resource (ie. "form_for [@category, @sub_category]" ). It seems that it''s calling a route, sub_category_path which I have never specified. The error comes up when an edit/create form is to be created where a form partial is called. I am really puzzled on why this is happening and have consulted the search results I had via google for ''nested resources with rspec'' , ''Rails in Action 3'' by Yehuda Katz, and the Rspec book :( If anyone knows what I am missing, I would love to hear your thoughts. thank you :) Gordon