2

Rails 3.1.0 Rspec 2

在嵌套资源的视图规范中,我是否需要在存根嵌套资源之前实例化/存根父资源?

我问这个是因为我在应用程序中引入的新嵌套资源的所有视图规范都失败了。当我手动测试嵌套资源时,它按预期工作:(这是我的编辑视图规范的样子。

----- "./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 ---------------------

这是我运行“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 ---------------------------

表单部分已正确匹配父资源和嵌套资源(即“ form_for [@category, @sub_category]”)。似乎它正在调用我从未指定的路线 sub_category_path 。

当要在调用表单部分的地方创建编辑/创建表单时,会出现错误。

我对为什么会发生这种情况感到非常困惑,并查阅了我通过谷歌获得的搜索结果,以获取“带有 rspec 的嵌套资源”、Yehuda Katz 的“Rails in Action 3”和 Rspec 书:(

如果有人知道我错过了什么,我很想听听你的想法。

4

1 回答 1

1

category_id不在您@sub_category.category_id@category视图中使用的位置(现在为零)。

[@category, @sub_category]只是一个快捷方式,url_for([@category, @sub_category])如果其中一个参数为 nil,则返回单个资源路径。

检查指南

于 2012-01-26T09:59:46.847 回答