LoginSignup
14
11

More than 3 years have passed since last update.

【rspec】confirmダイアログのテスト

Last updated at Posted at 2020-04-03

開発環境

ruby '2.6.3'
rails '6.0.2'

railsでconfirmダイアログのテストをする。

systemテスト
  context "削除する" do
    it "投稿消去" do
      click_button "消去する"
      expect{
        expect(page.accept_confirm).to eq "本当に削除しますか?"
        expect(page).to have_content "レビューを消去しました。"
        }. to change(@user.posts, :count).by(-1)
    end
  end

注意点

  • expectのブロック内にひとつ以上のexpectもしくはfindを入れないと、ダイアログが表示されてacceptされる前に次へ進んでしまうので注意が必要です。 つまり
systemテスト
  context "削除する" do
    it "投稿消去" do
      click_button "消去する"
      expect{
        expect(page.accept_confirm).to eq "本当に削除しますか?"
        expect(page).to have_content "レビューを消去しました。"
     # ↑この一文かsleepが必要です。
        }. to change(@user.posts, :count).by(-1)
    end
  end

その他

slim
= button_to "消去する",post_path(@post), method: :delete, data: {confirm: "本当に削除しますか?"}
コントローラー
  def destroy
    @post = current_user.posts.find(params[:id])
    flash[:notice] = "レビューを消去しました。"
    @post.destroy
    redirect_to user_path(current_user.id)
  end
14
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11