public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
refactored the mail assertions

I've remove assert_sends_email and replaced it with assert_sent_email.
tsaleh (author)
Sat Apr 26 14:01:37 -0700 2008
commit  816dbb061aac342e7f33bdba09a2e9792e8cb8c2
tree    1747fe22cb6a370baa2944ca9072cd3b14075476
parent  c9bd4f1c86b90761ad61b9f4e60de299cc62094b
...
88
89
90
91
 
 
92
93
94
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
97
98
...
88
89
90
 
91
92
93
 
 
 
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
0
@@ -88,11 +88,36 @@ module ThoughtBot # :nodoc:
0
         assert(num == ActionMailer::Base.deliveries.size, msg)
0
       end
0
 
0
- # Asserts that the block does not send emails thorough ActionMailer
0
+ # Asserts that an email was delivered. Can take a blog that can further
0
+ # narrow down the types of emails you're expecting.
0
       #
0
- # assert_does_not_send_email { # do nothing }
0
- def assert_does_not_send_email(&blk)
0
- assert_sends_email 0, &blk
0
+ # assert_sent_email
0
+ #
0
+ # passes if ActionMailer::Base.deliveries has an email
0
+ #
0
+ # assert_sent_email do |email|
0
+ # email.subject =~ /hi there/ && email.to == 'none@none.com'
0
+ # end
0
+ #
0
+ # passes if there is an email with subject containing 'hi there' and
0
+ # recipient equal to 'none@none.com'
0
+ #
0
+ def assert_sent_email
0
+ emails = ActionMailer::Base.deliveries
0
+ assert !emails.empty?, "No emails were sent"
0
+ if block_given?
0
+ matching_emails = emails.select {|e| yield e }
0
+ assert !matching_emails.empty?, "None of the emails matched."
0
+ end
0
+ end
0
+
0
+ # Asserts that no ActionMailer mails were delivered
0
+ #
0
+ # assert_did_not_send_email
0
+ def assert_did_not_send_email
0
+ msg = "Sent #{ActionMailer::Base.deliveries.size} emails.\n"
0
+ ActionMailer::Base.deliveries.each { |m| msg << " '#{m.subject}' sent to #{m.to.to_sentence}\n" }
0
+ assert ActionMailer::Base.deliveries.empty?, msg
0
       end
0
 
0
       def pretty_error_messages(obj)
...
1
2
3
4
5
6
7
 
 
8
9
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
12
13
...
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
0
@@ -1,13 +1,53 @@
0
 require File.join(File.dirname(__FILE__), '..', 'test_helper')
0
-
0
-class Val
0
- @@val = 0
0
- def self.val; @@val; end
0
- def self.inc(i=1); @@val += i; end
0
-end
0
+require 'action_mailer'
0
+require 'mocha'
0
 
0
 class HelpersTest < Test::Unit::TestCase # :nodoc:
0
 
0
+ context "given delivered emails" do
0
+ setup do
0
+ email1 = stub(:subject => "one", :to => ["none1@email.com"])
0
+ email2 = stub(:subject => "two", :to => ["none2@email.com"])
0
+ ActionMailer::Base.stubs(:deliveries).returns([email1, email2])
0
+ end
0
+
0
+ should "have sent an email" do
0
+ assert_sent_email
0
+
0
+ assert_raises(Test::Unit::AssertionFailedError) do
0
+ assert_did_not_send_email
0
+ end
0
+ end
0
+
0
+ should "find email one" do
0
+ assert_sent_email do |e|
0
+ e.subject =~ /one/
0
+ end
0
+ end
0
+
0
+ should "not find an email that doesn't exist" do
0
+ assert_raises(Test::Unit::AssertionFailedError) do
0
+ assert_sent_email do |e|
0
+ e.subject =~ /whatever/
0
+ end
0
+ end
0
+ end
0
+ end
0
+
0
+ context "when there are no emails" do
0
+ setup do
0
+ ActionMailer::Base.stubs(:deliveries).returns([])
0
+ end
0
+
0
+ should "not have sent an email" do
0
+ assert_did_not_send_email
0
+
0
+ assert_raises(Test::Unit::AssertionFailedError) do
0
+ assert_sent_email
0
+ end
0
+ end
0
+ end
0
+
0
   context "an array of values" do
0
     setup do
0
       @a = ['abc', 'def', 3]

Comments

    No one has commented yet.