2011-07-15 7 views
4

私はシンプルなテストをしていますが、足場が何を生成したのかは分かりませんが、なぜ動作しないのか分かりません。RSpecテストでコントローラが呼び出されない

describe AttachmentsController do 
    def mock_attachment(stubs={}) 
    @mock_attachment ||= mock_model(Attachment, stubs).as_null_object 
    end 

    describe "POST create" do 
    describe "with valid params" do 
     it "assigns a newly created attachment as @attachment" do 
     Attachment.stub(:new).with({'these' => 'params'}) { mock_attachment(:save => true) } 
     post :create,:attachment => {'these' => 'params'} 
     assigns(:attachment).should be(mock_attachment) 
     end 

が、これ(この仕様では、他のすべてのテスト)線に沿って何かで失敗します。

# POST /attachments 
    # POST /attachments.xml 
    def create 
    @attachment = Attachment.new(params[:attachment]) 
    @attachment.idea_id = params[:idea_id] 

    respond_to do |format| 
     if @attachment.save 
     format.html { redirect_to(idea_path(params[:idea_id]), :notice => 'Attachment was successfully created.') } 
     format.xml { render :xml => @attachment, :status => :created, :location => @attachment } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @attachment.errors, :status => :unprocessable_entity } 
     end 

    end 
    end 
end 

そしてスペック:私はAttachmentsControllerを持って

:ここでは状況があります

expected #<Attachment:33902000> => #<Attachment:0x2054db0 @name="Attachment_1001"> 
    got #<NilClass:4> => nil 

私が理解できない理由から、At​​tachmentsController#createは呼び出されていない。

ルートがあります:

POST /attachments(.:format)   {:action=>"create", :controller=>"attachments"} 

これは、ログが言うことである:

Processing by AttachmentsController#create as HTML 
    Parameters: {"attachment"=>{"these"=>"params"}} 
Rendered text template (0.0ms) 
Completed 302 Found in 52ms (Views: 23.1ms | ActiveRecord: 0.0ms) 

私も、私が作成したコードを呼び出すことができることに注意してください(と、それは素晴らしい作品)のウェブサイトを通じてそれだけでは失敗しているテストです。

これで、post()またはget()がこのようなコントローラを呼び出さない原因は何ですか?あなたがshould_receive試してみて、それが良い練習だと前のブロックにそれを置くことができ

+0

私はRSpecにはあまりよく慣れていませんが、 'mock_attachment'は' 'true'のようなsthで置き換えてテストを実行できますか? – Bohdan

+0

作成アクションコード全体を投稿できますか? – moritz

+0

あなたの作成コードがありません。それはあなたの質問の鍵です。 – lzap

答えて

3

:この質問の将来の視聴者のために

describe AttachmentsController do 
    describe "POST create" do 
    let(:attachment) { mock_attachment(:save => save_result) } 

    subject { post :create, :attachment => params } 

    before do 
     Attachment.should_receive(:new).and_return(attachment) 
    end 

    describe "with valid params" do 
     let(:attachment_params) { {'these' => 'params'} } 
     let(:save_result) { true } 

     it "assigns a newly created attachment as @attachment" do 
     assigns(:attachment).should be(mock_attachment) 
     end 
    end 
    end 
end 
+0

しかし、このアプローチでも、私はまだ同じ問題を抱えています - 投稿を呼び出す:決してAttachemntControllerを作成しないでください#create – lambinator

+10

ああ、私はログを見ました。このアクションを使用するには、ログインする必要がありますか?または、リダイレクトを行うフィルタの前に他のフィルタがいくつかありますか? – solnic

+0

うわー、はい..それです。ありがとうございました!! – lambinator

7

、実際の答えは受け入れ答えにコメントして@solnicにより投稿されました: ログを確認してください。この場合(そして私自身の状況では)リダイレクトが原因でこの問題が発生しましたが、これはログにしか表示されませんでした。

関連する問題