2013-08-30 32 views
0

我有這樣的失敗視圖規格:查看規格不運行控制器before_action

require 'spec_helper' 

describe "ideas/edit" do 
    before(:each) do 
    @idea = assign(:idea, stub_model(Idea, 
     :phase => 1, 
     :brief => "MyText", 
     :image => "MyString", 
     :active => true, 
     :activity_list => ["Science & Technology"] 
    )) 
    end 

    it "renders the edit idea form" do 
    render 

    # Run the generator again with the --webrat flag if you want to use webrat matchers 
    assert_select "form[action=?][method=?]", idea_path(@idea), "post" do 
     assert_select "textarea#idea_brief[name=?]", "idea[brief]" 
     assert_select "input#idea_industry[name=?]", "idea[industry]" 
    end 
    end 
end 

錯誤:

1) ideas/edit renders the edit idea form 
    Failure/Error: render 
    ActionView::Template::Error: 
     undefined method `empty?' for nil:NilClass 
    # ./app/views/ideas/_long_form.html.erb:27:in `block in _app_views_ideas__long_form_html_erb___781758430_98755320' 
    # ./app/views/ideas/_long_form.html.erb:1:in `_app_views_ideas__long_form_html_erb___781758430_98755320' 
    # ./app/views/ideas/edit.html.erb:3:in `_app_views_ideas_edit_html_erb__967817323_98657300' 
    # ./spec/views/ideas/edit.html.erb_spec.rb:15:in `block (2 levels) in <top (required)>' 

這是呈現形式的相關線路。 @categories變量在控制器中初始化,但在運行規範時由於某種原因而爲零。

<div class="field"> 
    <%= f.label :category_list, "Categories" %> 
    <%= f.select :category_list, @categories, {selected:@idea.category_list}, {:multiple => true, :style => "height:180px;"} %> 
</div> 

在我的控制,我有:

before_action :set_categories, only: [:edit, :update] 
... 
def set_categories 
     @categories = [ 
     "Arts & Entertainment", 
     "Science & Technology", 
     "Business & Finance", 
     "Software & Internet", 
     "Retail", 
     "Education", 
     "Energy & Utilities", 
     "Food & Health", 
     "Media & Communications", 
     "Other" 
     ] 
    end 

當故障排除後我試圖消除「唯一」的限制,並收到了同樣的錯誤。

有趣的是,該規範傳遞如果我下面的行添加到它的前(:每個)塊:

@categories = "Other" 

似乎該規範沒有運行在控制器中before_action。這是預期的行爲?我在這裏錯過了什麼?

回答

0

此信息指示該視圖規範旨在從控制器和該變量必須在spec被設置在隔離運行:http://www.arailsdemo.com/posts/37「實例變量呈現視圖和」

下添加此線右側在我的規格中「渲染」之前

assign :categories, []