私はシンプルなテストをしていますが、足場が何を生成したのかは分かりませんが、なぜ動作しないのか分かりません。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
私が理解できない理由から、AttachmentsController#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試してみて、それが良い練習だと前のブロックにそれを置くことができ
私はRSpecにはあまりよく慣れていませんが、 'mock_attachment'は' 'true'のようなsthで置き換えてテストを実行できますか? – Bohdan
作成アクションコード全体を投稿できますか? – moritz
あなたの作成コードがありません。それはあなたの質問の鍵です。 – lzap