From 9f926de54e1bd596d5c65cb45029ddb458cbb402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 10 Apr 2021 18:57:06 +0200 Subject: [PATCH] Refactor method to reply to comment in tests We take the comment as a parameter instead of the user, since usually people reply to comments and not to users. We also remove one database query after the browser has started, since we can use `debate_path(debate)`. It's also more clear why we're using `debate_path` in the test; before these changes, we had to enter the `reply_to` method to realize that we were replying on a debate. --- spec/support/common_actions/comments.rb | 8 +++----- spec/system/emails_spec.rb | 10 ++++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/spec/support/common_actions/comments.rb b/spec/support/common_actions/comments.rb index 008bf8ce6..370e16fd9 100644 --- a/spec/support/common_actions/comments.rb +++ b/spec/support/common_actions/comments.rb @@ -6,12 +6,10 @@ module Comments CommentNotifier.new(comment: comment).process end - def reply_to(original_user, replier: create(:user)) - debate = create(:debate) - comment = create(:comment, commentable: debate, user: original_user) - + def reply_to(comment, replier: create(:user)) login_as(replier) - visit debate_path(debate) + + visit polymorphic_path(comment.commentable) click_link "Reply" within "#js-comment-form-comment_#{comment.id}" do diff --git a/spec/system/emails_spec.rb b/spec/system/emails_spec.rb index d24776c5a..330869153 100644 --- a/spec/system/emails_spec.rb +++ b/spec/system/emails_spec.rb @@ -177,27 +177,29 @@ describe "Emails" do context "Comment replies" do let(:user) { create(:user, email_on_comment_reply: true) } + let(:debate) { create(:debate) } + let!(:comment) { create(:comment, commentable: debate, user: user) } scenario "Send email on comment reply" do - reply_to(user) + reply_to(comment) email = open_last_email expect(email).to have_subject("Someone has responded to your comment") expect(email).to deliver_to(user) - expect(email).not_to have_body_text(debate_path(Comment.first.commentable)) + expect(email).not_to have_body_text(debate_path(debate)) expect(email).to have_body_text(comment_path(Comment.last)) expect(email).to have_body_text("To stop receiving these emails change your settings in") expect(email).to have_body_text(account_path) end scenario "Do not send email about own replies to own comments" do - reply_to(user, replier: user) + reply_to(comment, replier: user) expect { open_last_email }.to raise_error("No email has been sent!") end scenario "Do not send email about comment reply unless set in preferences" do user.update!(email_on_comment_reply: false) - reply_to(user) + reply_to(comment) expect { open_last_email }.to raise_error("No email has been sent!") end end