Rails 3.1.0Rspec 2
在嵌套资源的视图规范中,是否需要实例化/stub在我存根嵌套资源之前的父资源?
我之所以问这个问题,是因为我所有的视图规格都无法用于新的我在应用程序中引入的嵌套资源。嵌套的资源工作符合预期当我手动测试它时:(以下是我的编辑视图规范的样子。
----- "./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 --------
以下是故障摘要:
----------- 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 -------------------------
这是我的表单部分看起来像
----- 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 ---------------------
以下是我运行"耙式路线"时看到的情况:
------- 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 ---------------------------
表单分部已与父资源正确匹配以及嵌套资源(即"form_for [@category, @sub_category]
")。它似乎在调用一条路由,sub_category_path,我有从未指定。
当创建编辑/创建表单时形式分部被称为。
我真的很困惑为什么会发生这种情况,并咨询了我通过谷歌搜索到的"带有rspec的嵌套资源"的搜索结果,Yehuda Katz的《Rails in Action 3》和Rspec书:(
如果有人知道我错过了什么,我很想听听你的想法。
您的category_id
在@sub_category.category_id
中,而不是在视图中使用的@category
中(现在为零)。
[@category, @sub_category]
只是url_for([@category, @sub_category])
的快捷方式,如果其中一个参数为零,则返回单个资源路径。
检查导轨。