forked from erica-bearica/rspec-rails-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
news_mailer_spec.rb
81 lines (62 loc) · 2.68 KB
/
news_mailer_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require "rails_helper"
RSpec.describe NewsMailer, :type => :mailer do
describe ".send_headlines(headlines:, to:)" do
it "has expected subject when there are headlines" do
mail = NewsMailer.send_headlines(
headlines: ["Scaremongering Headline: Be Fearful!"],
to: "[email protected]"
)
expect(mail).to have_subject("Today's Headlines")
end
[nil, []].each do |no_headlines|
it "has expected subject when there are no headlines '#{no_headlines.inspect}'" do
NewsMailer.send_headlines(headlines: no_headlines, to: "[email protected]")
expect(open_last_email).to have_subject "No Headlines Today :-("
end
end
it "sends from the default email" do
mail = NewsMailer.send_headlines(headlines: nil, to: "[email protected]")
expect(mail).to be_delivered_from("[email protected]")
end
it "sends to the given recipient" do
mail = NewsMailer.send_headlines(headlines: nil, to: "[email protected]")
expect(mail).to be_delivered_to("[email protected]")
end
it "calls the error-sensitive deliver_now!" do
delivery = double
expect(delivery).to receive(:deliver_now!).with(no_args)
expect(NewsMailer)
.to receive(:headlines)
.with(headlines: ["Man Bites Dog!"], to: "[email protected]")
.and_return(delivery)
NewsMailer.send_headlines(headlines: ["Man Bites Dog!"], to: "[email protected]")
end
context "HTML body" do
it "has the given headlines" do
headlines = ["A headline", "Another headline"]
mail = NewsMailer.send_headlines(headlines: headlines, to: "[email protected]")
headlines.each do |headline|
html = %Q|<li>#{headline}</li>|
expect(mail).to have_body_text(html)
end
end
it "contains no headline message when no headlines" do
mail = NewsMailer.send_headlines(headlines: [], to: "[email protected]")
expect(mail).to have_body_text("<p>Sorry, no headlines. Better luck next time.</p>")
end
end
context "plain text body" do
it "has the given headlines" do
headlines = ["A headline", "Another headline"]
mail = NewsMailer.send_headlines(headlines: headlines, to: "[email protected]")
headlines.each do |headline|
expect(mail.text_part).to have_body_text("- #{headline}")
end
end
it "contains no headline message when no headlines" do
mail = NewsMailer.send_headlines(headlines: [], to: "[email protected]")
expect(mail.text_part).to have_body_text("Sorry, no headlines. Better luck next time.")
end
end
end
end