LoginSignup
5
0

More than 1 year has passed since last update.

[Rspec / system_spec]記事一覧ページの削除ボタンをテストする

Last updated at Posted at 2021-06-16

記事一覧から削除ボタンを押して記事を削除する、
という機能をテストするのにちょっと突っかかった備忘録です。

Left align バージョン
OS macOS Big Sur 11.3
Ruby 3.0.0
Rails 6.1.3.1
Rspec 3.10

capybaraさんに怒られた内容

1) Articles deleting deletes an article from index page
     Failure/Error: expect(page).to have_current_path local_articles_path(local)

     Selenium::WebDriver::Error::UnexpectedAlertOpenError:
       unexpected alert open: {Alert text : MyStringを削除します}
         (Session info: headless chrome=91.0.4472.101)

そのわけ

ビューで

app/views/articles/_article.html.erb
<li>
<!-- 略 -->
<span class="delete_button"><%= link_to '削除', article, method: :delete, data: {confirm: "'#{article.title}' を削除します"} %>
</li>

としていたうちの、
{confirm: "'#{article.title}' を削除します"}について
「そんなの聞いてないよー!」と怒られたわけです。

それもそのはず

こうなってました。

spec/system/articles_spec.rb
RSpec.describe 'Articles', js: true, type: :system do
  let(:local) { FactoryBot.create(:local) }
  let(:article) { FactoryBot.build(:article, local: local) }
  let(:another_article) { FactoryBot.create(:article, local: local)}
  before do
    local.confirm
    @number_of_articles = Article.count
    @number_of_tags = Tag.count
  end

......

  describe 'deleting' do
    it 'deletes an article from index page' do
      article.save
      sign_in local
      visit local_articles_path(local)
       #------- ここから
          within "li#article-#{article.id}" do
            click_link '削除'
          end
        #------- ここまでの話
      expect(page).to have_current_path local_articles_path(local)
    end
  end
end

とりあえず「削除ボタン押して」しかcapybaraさんに指示出しできてませんでした。

accept_alertを追記します。

spec/system/articles_spec.rb
RSpec.describe 'Articles', js: true, type: :system do
  let(:local) { FactoryBot.create(:local) }
  let(:article) { FactoryBot.build(:article, local: local) }
  let(:another_article) { FactoryBot.create(:article, local: local)}
  before do
    local.confirm
    @number_of_articles = Article.count
    @number_of_tags = Tag.count
  end

......

  describe 'deleting' do
    it 'deletes an article from index page' do
      article.save
      sign_in local
      visit local_articles_path(local)
      accept_alert do #------- ここから
        within "li#article-#{article.id}" do
          click_link '削除'
        end
      end              #------- ここまでの話
      expect(page).to have_current_path local_articles_path(local)
    end
  end
end

参考

5
0
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
5
0