Fix Newsletter Mailer#464
Conversation
spacepotato
left a comment
There was a problem hiding this comment.
Awesome work on this! Let me know if you'd like to pair on any of the spec changes.
Before opening this up for a more detailed review I'd also recommend checking out: https://github.com/rubyaustralia/ruby_au/blob/main/CONTRIBUTING.md#submitting-your-contribution to make sure that you've ticked all of the contributing requirements.
|
|
||
| def primary_email | ||
| emails.find_by(primary: true)&.email || email | ||
| emails.find_by(primary: true)&.email || email.presence || read_attribute_before_type_cast("email").presence |
There was a problem hiding this comment.
@audreeedale can you add some context to this change?
| require "rails_helper" | ||
|
|
||
| RSpec.describe CampaignsMailer, type: :mailer do | ||
| describe '#campaign_email' do |
There was a problem hiding this comment.
@audreeedale I think we can make the tests a fair bit slimmer if we hoist some of the common values up to let declarations.
Looking at the logic for campaign_email it looks like the following things are true:
- The membership only exists to identify the user
- We only need the user to access the attached emails
So with that in mind we could make the first spec something like this:
describe '#campaign_email' do
let!(:user) { create(:user) }
let!(:membership) { create(:membership, user:) }
let!(:campaign) { create(:campaign, subject: 'Test campaign') }
let(:primary_email) { create(:email, user:, email: 'primary@test.com', primary: true) }
let(:default_email) { create(:email, user:, email: 'default@test.com', primary: false) }
it "sends to the user's primary email" do
primary_email && default_email
mail = described_class.campaign_email(campaign, membership, nil).deliver_now
expect(mail.to).to eq(primary_email.email)
end
endI've deliberately used let (lazy loaded) instead of let! (eager loaded) because it makes the second spec where we don't want the primary email to exist easier to implement.
What
Why
Links
#398