Module: ActionMailerMatchers

Extended by:
RSpec::Matchers::DSL
Defined in:
lib/action_mailer_matchers.rb,
lib/action_mailer_matchers/version.rb

Constant Summary collapse

VERSION =
"1.2.0"

Instance Method Summary collapse

Instance Method Details

#body_for(mail) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/action_mailer_matchers.rb', line 45

def body_for(mail)
  if mail.try(:parts).present?
    mail.try(:parts).try(:first).try(:body).try(:raw_source)
  else
    mail.try(:body).try(:raw_source)
  end
end

#deliveriesObject



53
54
55
# File 'lib/action_mailer_matchers.rb', line 53

def deliveries
  ActionMailer::Base.deliveries
end

#matching_mail_for(email, body, email_subject, email_method) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/action_mailer_matchers.rb', line 35

def matching_mail_for(email, body, email_subject, email_method)
  deliveries.detect do |mail|
    has_expected_email = mail.send(email_method).include?(email)
    has_expected_body = body ? body_for(mail).include?(body) : true
    has_expected_subject = email_subject ? mail.subject.include?(email_subject) : true

    has_expected_email && has_expected_body && has_expected_subject
  end
end

#received_email?(email_or_user, expected, email_method = :to) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
# File 'lib/action_mailer_matchers.rb', line 24

def received_email?(email_or_user, expected, email_method = :to)
  expected_attrs = expected.present? ? expected : {}
  email = email_or_user.try(:email) || email_or_user
  email_subject = expected_attrs[:subject]
  body = expected_attrs[:body]

  matching_email = matching_mail_for(email, body, email_subject, email_method)

  expect(matching_email.present?).to eql(true)
end