2012-01-05 8 views
1

私はacts_as_auditedを使用しています。 destroyアクションのコントローラーでは、監査コメント値を渡します。このすべてがうまく動作しますが、私は破壊するアクションをテストしようとすると、私が取得:フォームパラメータをrspecに渡す

PurchasesController DELETE /destroy deletes the correct Purchase 
Failure/Error: delete :destroy, id: i 
NoMethodError: 
    You have a nil object when you didn't expect it! 
    You might have expected an instance of Array. 
    The error occured while evaluating nil.[] 
# ./app/controllers/purchases_controller.rb:79:in `destroy' 
# ./spec/controllers/purchases_controller_spec.rb:156:in `block (3 levels) in <top (required)>' 

ライン#79は、読み取ります@purchase.audit_comment = params[:purchase][:audit_comment]

HERESに私のコード:

PurchasesController

def destroy 
    @purchase = Purchase.find(params[:id]) 
    @purchase.audit_comment = params[:purchase][:audit_comment] 
    respond_to do |format| 
    if @purchase.destroy 
     format.html { redirect_to(purchases_url, notice: "Successfully destroyed purchase.") } 
     format.xml { render :xml => @purchase, :status => :deleted, :location => @purchase } 
    else 
     flash[:alert] = "#{@purchase.po_number} could not be destroyed" 
     render 'show' 
    end 
    end 
end 

購入show.html.erb EDITED

<%= form_for @purchase, method: :delete do |builder| %> 

<% if @purchase.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@purchase.errors.count, "error") %> prohibited this purchase from being saved:</h2> 

    <ul> 
    <% @purchase.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
<% end %> 

<% title "Purchase" %> 

<div id="delete_button">  
    <span><strong>PO Number: <%= @purchase.po_number %></strong></span> 
    <%= render "audit_comment", f: builder %> 
    <%= builder.submit "Destroy Purchase"%> 
</div> 
<% end %> 
<p> 
    <%= link_to "Edit", edit_purchase_path(@purchase) %> | 
    <%= link_to "View All", purchases_path %> 
</p> 

_audit_comment.html.erb - 購入

<div id = "audit"> 
    <%= f.label :audit_comment %>:<br /> 
    <%= f.text_area :audit_comment, :size => "60x5" %> 
</div> 

purchase_controller_spec.rb

require 'spec_helper' 
require 'ruby-debug' 

describe PurchasesController do 
    login_user 
    render_views 

    before(:each) do 
    @purchase = Factory(:purchase) 
    end 

    describe "DELETE /destroy" do 
    before(:each) do 
     @ability.can :destroy, Purchase  
    end 
    it "deletes the correct Purchase" do 
     i = @purchase.id 
     c = Purchase.count 
     pl = Purchase.find(i).purchase_line_items 
     cpl = pl.count 
     delete :destroy, id: i 
     Purchase.count.should == c-1 
     pl.count.should == cpl-1 
     response.should redirect_to(purchases_path) 
     flash[:notice].should == "Successfully destroyed purchase." 
    end 
    it "redirects to the index page with an alert when a delete fails" do 
     i = @purchase.id 
     c = Purchase.count 
     pl = Purchase.find(i).purchase_line_items 
     cpl = pl.count 
     Purchase.any_instance.stubs(:valid?).returns(:false) 
     delete :destroy, id: i 
     Purchase.count.should_not == c-1 
     pl.count.should_not == cpl-1 
     response.should render_template('show') 
     flash[:alert].should == "#{@purchase.po_number} could not be destroyed" 
    end 
    end 
end 

任意の助けが理解されます。ありがとう!

答えて

2

これは、79行目でparams[:purchase]がnilであることを示しています。

理由がないのは、button_toが独自のフォームタグを生成するからです。したがって、今<form>内に<form>があり、監査コメントフィールドが送信されていません。 button_toの代わりにbuilder.submitを使用してください。また、form_forへの電話で:methodオプションを設定して、DELETEリクエストにする必要があります。

HTMLが今OKに見えますが、私はスペックの問題を見に疑問を編集した後

を更新。私はあなたのHTTP paramsに監査コメントを渡すことを忘れていると思います。これはHTMLにありますが、仕様をバイパスするのは、コントローラを単独でテストするためです。 (統合テストでは実際のフォームが使用されますが、コントローラテストではそうではありません)。したがって、コントローラーが期待するフォームパラメータを手動でリクエストに追加する必要があります。例:

delete :destroy, :id => 1, :purchase => {:audit_comment => 'Test comment'} 
+0

フォームを変更しましたが、監査コメントがテキスト領域にあるため、購入を削除することができます。唯一のことは、同じエラーでテストが失敗することです。お返事をありがとうございます。 – brink

+0

新しいHTMLで質問を編集できますか?私はまだそれにbutton_toがあるのを見る。もしあなたが今持っているものを見ることができれば、おそらく私はエラーが何であるかを判断することができます。 – rlkw1024

+0

showページには新しいHTMLがあります。 – brink

関連する問題