From 971571b54b502bb0ec1a7180ddcaa225f533f94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 18 Nov 2019 15:47:47 +0100 Subject: [PATCH] Simplify testing followables flash messages Checking the whole text is tricky because the text has a `
` tag, and now Capybara doesn't normalize whitespace by default anymore. Here are a couple more options we could use: ``` expect(page).to have_content strip_tags(message.gsub(/\s*
\s*/,"\n")) expect(page).to have_content strip_tags(message), normalize_ws: true ``` But then developers would wonder why we're doing all this, and would need an extra effort to fully understand the test. Since the tests are only checking the presence of the flash message, checking a relevant part of the test is enough, works with any version of Capybara, and makes the test easy to follow. --- spec/shared/features/followable.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/shared/features/followable.rb b/spec/shared/features/followable.rb index 6fd24d236..8bfbca12b 100644 --- a/spec/shared/features/followable.rb +++ b/spec/shared/features/followable.rb @@ -55,14 +55,13 @@ shared_examples "followable" do |followable_class_name, followable_path, followa scenario "Should display new follower notice after user clicks on follow button", :js do user = create(:user) login_as(user) - create_notice_message = t("shared.followable.#{followable_class_name}.create.notice") visit send(followable_path, arguments) within "##{dom_id(followable)}" do click_link("Follow #{followable.model_name.human.downcase}") end - expect(page).to have_content strip_tags(create_notice_message) + expect(page).to have_content "We will notify you of changes as they occur" end scenario "Display unfollow button when user already following" do @@ -90,14 +89,13 @@ shared_examples "followable" do |followable_class_name, followable_path, followa scenario "Should display destroy follower notice after user clicks on unfollow button", :js do user = create(:user, followables: [followable]) login_as(user) - destroy_notice_message = t("shared.followable.#{followable_class_name}.destroy.notice") visit send(followable_path, arguments) within "##{dom_id(followable)}" do click_link("Unfollow #{followable.model_name.human.downcase}") end - expect(page).to have_content strip_tags(destroy_notice_message) + expect(page).to have_content "You will no longer receive notifications" end end end