2017-07-05 12 views
0

"csstests"を作成して破棄するRails 5アプリケーションがあります。私はコントローラがアクションを破壊するためのテストを書こうとしています。RSpecテストのアクションを破棄して成功を返さない

ここ
def destroy 
    @test = Csstest.find(params[:id]) 
    if @test.destroy 
     flash.now[:success] = "Your test was removed" 
     redirect_to csstests_path 
    end 
end 

はRSpecのテストである:ここでアクションがある

describe 'DELETE #destroy' do 

    before do 
     test1, test2 = Csstest.create, Csstest.create 
     delete :destroy, params: { id: test1.id } 
    end 

    it 'returns HTTP success' do 
     expect(response).to be_success 
     expect(response).to return_http_status(200) 
    end 

end 

これは私が失敗から得るメッセージです:

1) CsstestsController DELETE #destroy returns HTTP success 
Failure/Error: expect(response).to be_success 
    expected `#<ActionDispatch::TestResponse:0x007fb1362beb68 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mu...spatch::Http::Headers:0x007fb1362de4b8 @req=#<ActionController::TestRequest:0x007fb1362becf8 ...>>>>.success?` to return true, got false 
HTTP codesの基礎の上にまで読む

答えて

0

- 特に:

  • 2xxレスポンスは「成功」ステータスコードです。
  • 3xxレスポンスは「リダイレクト」ステータスコードです。お使いのコントローラで

、あなたはCsstestレコードが削除された場合にリダイレクトを実行することを選択した:redirect_to csstests_path

これは本当に必要な動作である場合、あなたはこれを期待して、あなたのrspecテストを更新できます。

expect(response).to redirect_to(csstests_path) 
# This second assertion is probably not really necessary 
# But I'll leave it here for completion... 
expect(response).to return_http_status(302) 

明示的にリダイレクト用301または303ステータスコードを返すことも可能であるが(the documentationを参照してください)。しかし、これもまた「成功」コードとはみなされません。