1
私はRails 4.2とRspec 3.4を使用しています。私はフラッシュメッセージをテストして正しいメッセージが表示されているかどうかを確認する3つのコントローラテストを行っています。 2人は働いていて1人は働いていません。私は働く人と働かない人の違いを見ることはできません。Rspec 3.4/Rails 4.2:フラッシュメッセージのテストが1つの仕様で失敗しましたが、他の2つではありません
私のコントローラは次のようになります。
describe "DELETE #destroy" do
### This test fails
it "as devcomm when user is owner returns success" do
devcomm_user
delete :destroy, id: documents(:nonsecretdocument1)
expect(response).to redirect_to(action: :index)
expect(flash[:notice]).to match "Document successfully deleted."
end
### This test passes
it "as devcomm when user is not owner redirects to documents#index" do
devcomm_user
delete :destroy, id: documents(:nonsecretdocument2)
expect(response).to redirect_to(action: :index)
expect(flash[:error]).to match "Could not delete document because you are not an owner."
end
### This test passes
it "as nondevcomm redirects to root_url" do
nondevcomm_user
delete :destroy, id: documents(:nonsecretdocument2)
expect(response).to redirect_to(root_url)
expect(flash[:error]).to match "You must be a Devcomm team member to perform this action."
end
end
エラー:
Failures:
1) DocumentsController DELETE #destroy as devcomm when user is owner returns success
Failure/Error: expect(flash[:notice]).to match "Document successfully deleted."
expected nil to match "Document successfully deleted."
# ./spec/controllers/documents_controller_spec.rb:114:in `block (3 levels) in <top (required)>'
最初のテストが失敗し、なぜ誰もが見ることができる
before_filter :logged_in_as_team_member, except: [:index, :show, :choose_revisions_to_diff, :diff_results]
def destroy
doc = Document.find(params[:id])
if doc.owners.include?(@user) && !doc.document_revisions.any?
doc.destroy
redirect_to documents_url, notice: "Document successfully deleted."
else
redirect_to documents_url, flash: { error: "Could not delete document because you are not an owner or the document has revisions assigned to it." }
end
end
private
def logged_in_as_team_member
redirect_to root_url, flash: { error: "You must be a Devcomm team member to perform this action." } unless @user.is_devcomm
end
私のRSpecのは、このようになります他の2つのパス?
編集済み:UIでドキュメントを削除すると、Flashが期待通りに表示されます。
コントローラアクションで変更しているレコードをテストすると便利です。この場合、'expect{@record.reload}.to raise_error(ActiveRecord :: RecordNotFound) 'のように、 –