about summary refs log tree commit diff
path: root/spec/mailers
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2022-01-28 00:46:42 +0100
committerGitHub <noreply@github.com>2022-01-28 00:46:42 +0100
commite38fc319dc6897ca867a509b0c7a5878d34d0f00 (patch)
treea907ef55275a6d61a201cb0e31c61c75a3a8ffd4 /spec/mailers
parent03d59340da3ffc380d7b27169bdc887140d88bee (diff)
Refactor and improve tests (#17386)
* Change account and user fabricators to simplify and improve tests

- `Fabricate(:account)` implicitly fabricates an associated `user` if
  no `domain` attribute is given (an account with `domain: nil` is
  considered a local account, but no user record was created), unless
  `user: nil` is passed
- `Fabricate(:account, user: Fabricate(:user))` should still be possible
  but is discouraged.

* Fix and refactor tests

- avoid passing unneeded attributes to `Fabricate(:user)` or
  `Fabricate(:account)`
- avoid embedding `Fabricate(:user)` into a `Fabricate(:account)` or the other
  way around
- prefer `Fabricate(:user, account_attributes: …)` to
  `Fabricate(:user, account: Fabricate(:account, …)`
- also, some tests were using remote accounts with local user records, which is
  not representative of production code.
Diffstat (limited to 'spec/mailers')
-rw-r--r--spec/mailers/admin_mailer_spec.rb8
-rw-r--r--spec/mailers/notification_mailer_spec.rb2
2 files changed, 7 insertions, 3 deletions
diff --git a/spec/mailers/admin_mailer_spec.rb b/spec/mailers/admin_mailer_spec.rb
index 4a8ef7b5e..29fb586a3 100644
--- a/spec/mailers/admin_mailer_spec.rb
+++ b/spec/mailers/admin_mailer_spec.rb
@@ -4,11 +4,15 @@ require 'rails_helper'
 
 RSpec.describe AdminMailer, type: :mailer do
   describe '.new_report' do
-    let(:sender)    { Fabricate(:account, username: 'John', user: Fabricate(:user)) }
-    let(:recipient) { Fabricate(:account, username: 'Mike', user: Fabricate(:user, locale: :en)) }
+    let(:sender)    { Fabricate(:account, username: 'John') }
+    let(:recipient) { Fabricate(:account, username: 'Mike') }
     let(:report)    { Fabricate(:report, account: sender, target_account: recipient) }
     let(:mail)      { described_class.new_report(recipient, report) }
 
+    before do
+      recipient.user.update(locale: :en)
+    end
+
     it 'renders the headers' do
       expect(mail.subject).to eq("New report for cb6e6126.ngrok.io (##{report.id})")
       expect(mail.to).to eq [recipient.user_email]
diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb
index 9b645bad8..2ca4e26fa 100644
--- a/spec/mailers/notification_mailer_spec.rb
+++ b/spec/mailers/notification_mailer_spec.rb
@@ -1,7 +1,7 @@
 require "rails_helper"
 
 RSpec.describe NotificationMailer, type: :mailer do
-  let(:receiver)       { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
+  let(:receiver)       { Fabricate(:user) }
   let(:sender)         { Fabricate(:account, username: 'bob') }
   let(:foreign_status) { Fabricate(:status, account: sender, text: 'The body of the foreign status') }
   let(:own_status)     { Fabricate(:status, account: receiver.account, text: 'The body of the own status') }