thumblemonks-thumblemonks-shoulda_action_mailer 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.markdown +4 -0
- data/LICENSE +24 -0
- data/README.markdown +48 -0
- data/lib/shoulda_action_mailer.rb +180 -0
- data/shoulda_action_mailer.gemspec +34 -0
- metadata +65 -0
data/HISTORY.markdown
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
MIT License
|
2
|
+
--------------------------------------------------------------------------------
|
3
|
+
Copyright (c) 2009 Thumble Monks, Justin Knowlden
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person
|
6
|
+
obtaining a copy of this software and associated documentation
|
7
|
+
files (the "Software"), to deal in the Software without
|
8
|
+
restriction, including without limitation the rights to use,
|
9
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the
|
11
|
+
Software is furnished to do so, subject to the following
|
12
|
+
conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
19
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
21
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
22
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
23
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
24
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Action Mailer Shoulda Macros (and maybe assertions, too)
|
2
|
+
|
3
|
+
While Shoulda does ship with some Action Mailer assertions, it does not ship with standard macros that would help in testing Action Mailer emails. Macros such as:
|
4
|
+
|
5
|
+
* should\_have\_subject
|
6
|
+
* should\_have\_to\_recipient
|
7
|
+
* should\_have\_to\_recipients - *notice the plurality!*
|
8
|
+
* should\_have\_from
|
9
|
+
* should\_have\_reply\_to
|
10
|
+
* should\_match\_body
|
11
|
+
* should\_have\_mime\_parts - *also with counts*
|
12
|
+
* should\_have\_mime\_content\_type
|
13
|
+
* should\_use\_charset
|
14
|
+
* should\_be\_inlined_content
|
15
|
+
* should\_be\_an\_attachment - *also with a check for filename*
|
16
|
+
* should\_have\_filename
|
17
|
+
|
18
|
+
Incidentally, those happen to be all the macros supported for now. We fully intend to have macros for everything in Action Mailer, but didn't have a need for any others yet (read: nothing forced us to write them yet). So, if you have a need for some others, please feel MORE THAN FREE to fork, add, and send a pull request so we can add them in.
|
19
|
+
|
20
|
+
For documentation, just look at the only file in the lib directory for now. Or, install the gem and the rdoc with it.
|
21
|
+
|
22
|
+
### Installing & Using
|
23
|
+
|
24
|
+
Install as a gem:
|
25
|
+
|
26
|
+
sudo gem install thumblemonks-shoulda_action_mailer
|
27
|
+
|
28
|
+
And then do this in your `test_helper.rb` (or whatever you call it for your environment):
|
29
|
+
|
30
|
+
require 'shoulda_action_mailer'
|
31
|
+
|
32
|
+
Or, just tell Rails about them by doing this in your app's `test.rb`:
|
33
|
+
|
34
|
+
config.gem 'thumblemonks-shoulda_action_mailer', :lib => false, :source => 'http://gems.github.com'
|
35
|
+
|
36
|
+
# Uh ... where are the tests?
|
37
|
+
|
38
|
+
I feel kind of bad not having tests here, but I actually did write these macros as refactorings to tests I already had in a certain other project that shall remain nameless for the time being. So, I don't really feel compelled to add any tests here unless someone asks me or I just decide to modify this code for random reason.
|
39
|
+
|
40
|
+
I hope that satisfies you.
|
41
|
+
|
42
|
+
# Contact
|
43
|
+
|
44
|
+
Justin Knowlden <gus@gusg.us>
|
45
|
+
|
46
|
+
# License
|
47
|
+
|
48
|
+
See LICENSE
|
@@ -0,0 +1,180 @@
|
|
1
|
+
module Thumblemonks #:nodoc:
|
2
|
+
module ActionMailer
|
3
|
+
# Every should_action_mailer macro assumes an instance variable named @email is defined and
|
4
|
+
# is an instance of a TMail object. Even the ones that work on parts.
|
5
|
+
#
|
6
|
+
# When we support Shoulda 2.10.2, we will switch to subject
|
7
|
+
module Shoulda
|
8
|
+
# Tries to match the provided subject with that of the email's. Regular
|
9
|
+
# expressions are valid inputs.
|
10
|
+
#
|
11
|
+
# should_have_subject 'Hello world'
|
12
|
+
#
|
13
|
+
# If a block is provided, the result of calling it will
|
14
|
+
# be used as the expected subject.
|
15
|
+
#
|
16
|
+
# should_have_subject { "What's going on #{@person.name}?" }
|
17
|
+
def should_have_subject(subject=nil, &block)
|
18
|
+
should "have subject" do
|
19
|
+
subject = self.instance_eval(&block) if block_given?
|
20
|
+
assert_match subject, @email.subject
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Asserts that the provided address is one of those in the list of to recipients.
|
25
|
+
#
|
26
|
+
# should_have_to_recipient "foo@bar.baz"
|
27
|
+
#
|
28
|
+
# If you provide a block, it will be used to obtain the recipient. If you need to use
|
29
|
+
# block approach more than once within the same context, use should_have_to_recipients
|
30
|
+
# instead.
|
31
|
+
#
|
32
|
+
# should_have_to_recipient { @person.email }
|
33
|
+
def should_have_to_recipient(recipient=nil, &block)
|
34
|
+
should "have #{recipient || 'some email address'} as a to recipient" do
|
35
|
+
recipient = self.instance_eval(&block) if block_given?
|
36
|
+
assert_contains @email.to, recipient
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Asserts that all of the to recipients match those in the provided list.
|
41
|
+
#
|
42
|
+
# should_have_to_recipients ["foo@bar.baz", "ma@pa.biz"]
|
43
|
+
#
|
44
|
+
# If you provide a block, it will be used to obtain the expected list of recipients.
|
45
|
+
#
|
46
|
+
# should_have_to_recipients { @network.people.map(&:email) }
|
47
|
+
def should_have_to_recipients(recipient_list=nil, &block)
|
48
|
+
handle_a_list("to recipients", :to, recipient_list, &block)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Asserts that all of the from addresses match those in the provided list. You can provide a
|
52
|
+
# single email address or an array of email addresses.
|
53
|
+
#
|
54
|
+
# should_have_from "foo@bar.baz"
|
55
|
+
# should_have_from ["foo@bar.baz", "ma@pa.biz"]
|
56
|
+
#
|
57
|
+
# If you provide a block, it will be used to obtain the list of from addresses.
|
58
|
+
#
|
59
|
+
# should_have_from { @person.email }
|
60
|
+
# should_have_from { [@network.owner.email, @you.mom.email] }
|
61
|
+
def should_have_from(from_list=nil, &block)
|
62
|
+
handle_a_list("from", :from, from_list, &block)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Asserts that all of the reply to addresses match those in the provided list. You can provide a
|
66
|
+
# single email address or an array of email addresses.
|
67
|
+
#
|
68
|
+
# should_have_reply_to "foo@bar.baz"
|
69
|
+
# should_have_reply_to ["foo@bar.baz", "ma@pa.biz"]
|
70
|
+
#
|
71
|
+
# If you provide a block, it will be used to obtain the list of reply to addresses.
|
72
|
+
#
|
73
|
+
# should_have_reply_to { @person.email }
|
74
|
+
# should_have_reply_to { [@network.owner.email, @you.mom.email] }
|
75
|
+
def should_have_reply_to(reply_to_list=nil, &block)
|
76
|
+
handle_a_list("reply to's", :reply_to, reply_to_list, &block)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Asserts that the email or part body matches the value provided.
|
80
|
+
#
|
81
|
+
# should_match_body /Sincerely,\nYour Mom/
|
82
|
+
# should_match_body %r[Dear John,]
|
83
|
+
# should_match_body "this is the entire email body" # bascially like an assert_equal
|
84
|
+
#
|
85
|
+
# If you provide a block, it will be used to get the expression that will be matched
|
86
|
+
# against the email body. You must provide a namespace for the test in this case, so
|
87
|
+
# test name collisions can be avoided (because we're assuming you're going to do this
|
88
|
+
# more than once within a context).
|
89
|
+
#
|
90
|
+
# should_match_body("salutation") { %r[Dear #{@person.name}] }
|
91
|
+
def should_match_body(matcher_or_name, &block)
|
92
|
+
should "match body to #{matcher_or_name}" do
|
93
|
+
matcher_or_name = self.instance_eval(&block) if block_given?
|
94
|
+
assert_match matcher_or_name, @email.body
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Asserts that the email has at least one part attached to it.
|
99
|
+
#
|
100
|
+
# should_have_mime_parts
|
101
|
+
#
|
102
|
+
# If provided with option `:count`, asserts that the exact number of parts are attached.
|
103
|
+
#
|
104
|
+
# should_have_mime_parts :count => 2
|
105
|
+
def should_have_mime_parts(opts={})
|
106
|
+
if count_of_parts = opts[:count]
|
107
|
+
should "have #{count_of_parts} message part(s)" do
|
108
|
+
assert_equal count_of_parts, Array(@email.parts).length
|
109
|
+
end
|
110
|
+
else
|
111
|
+
should("have at least one message part") { assert Array(@email.parts).length > 0 }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Asserts that the content type of the email or part matches the expectation.
|
116
|
+
#
|
117
|
+
# should_have_mime_content_type "text/plain"
|
118
|
+
# should_have_mime_content_type "multipart/alternative" # For multi-part emails without attachments
|
119
|
+
# should_have_mime_content_type "multipart/mixed" # For multi-part emails with attachments
|
120
|
+
# should_have_mime_content_type "text/csv" # For file attachments
|
121
|
+
def should_have_mime_content_type(name)
|
122
|
+
should "have #{name} content type" do
|
123
|
+
@email.to_s # Not doing this means the content-type is never generated (i smell bug)
|
124
|
+
assert_equal name, @email["content-type"].content_type
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
# Asserts that the charset of the email or part matches the expectation. Expects the charset to be
|
129
|
+
# defined on the Content-Type header.
|
130
|
+
#
|
131
|
+
# should_have_mime_content_type "text/utf-8"
|
132
|
+
def should_use_charset(name)
|
133
|
+
should("use #{name} charset") { assert_equal name, @email["content-type"]["charset"] }
|
134
|
+
end
|
135
|
+
|
136
|
+
# Asserts that the content-disposition of the part is "inline". Meaning, the content is to be displayed
|
137
|
+
# in the readers viewer immediately (different than an attachment). Basically, you've put some text in
|
138
|
+
# the email you want the reader to see.
|
139
|
+
#
|
140
|
+
# should_be_inlined_content
|
141
|
+
def should_be_inlined_content
|
142
|
+
should("be inline content") { assert_equal "inline", @email["content-disposition"].disposition }
|
143
|
+
end
|
144
|
+
|
145
|
+
# Asserts that the content-disposition of the part is "attachment". Meaning, the reader has to do
|
146
|
+
# something to see the content. Whenever you use the `ActionMailer::Base#attachment` method, you'll
|
147
|
+
# get this disposition.
|
148
|
+
#
|
149
|
+
# should_be_an_attachment
|
150
|
+
#
|
151
|
+
# If you provide a filename option, this method will assert that the attachment filename matches your
|
152
|
+
# expectation.
|
153
|
+
#
|
154
|
+
# should_be_an_attachment :filename => "file.pdf"
|
155
|
+
def should_be_an_attachment(opts={})
|
156
|
+
should("be an attachment") { assert_equal "attachment", @email["content-disposition"].disposition }
|
157
|
+
should_have_filename(opts[:filename]) if opts[:filename]
|
158
|
+
end
|
159
|
+
|
160
|
+
# Asserts that the attachment (most likely) has a filename attribute and that it matches the expected
|
161
|
+
# filename.
|
162
|
+
#
|
163
|
+
# should_have_filename "file.pdf"
|
164
|
+
def should_have_filename(filename)
|
165
|
+
should "have filename of #{filename}" do
|
166
|
+
assert_equal filename, @email["content-disposition"]["filename"]
|
167
|
+
end
|
168
|
+
end
|
169
|
+
private
|
170
|
+
def handle_a_list(named, accessed_as, expected=nil, &block)
|
171
|
+
should "have #{named}" do
|
172
|
+
expected = self.instance_eval(&block) if block_given?
|
173
|
+
assert_same_elements Array(expected), Array(@email.send(accessed_as))
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end # Shoulda
|
177
|
+
end # ActionMailer
|
178
|
+
end # Thumblemonks
|
179
|
+
|
180
|
+
ActionMailer::TestCase.extend(Thumblemonks::ActionMailer::Shoulda)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "thumblemonks-shoulda_action_mailer"
|
3
|
+
s.version = "0.2"
|
4
|
+
s.date = "2009-08-26"
|
5
|
+
s.summary = "Shoulda macros and more assertions for Action Mailer and ActionMailer::TestCase; now includes tests for parts."
|
6
|
+
s.email = %w[gus@gusg.us]
|
7
|
+
s.homepage = "http://github.com/thumblemonks/shoulda_action_mailer"
|
8
|
+
s.description = "Shoulda macros and more assertions for Action Mailer and ActionMailer::TestCase; now includes tests for parts."
|
9
|
+
s.authors = %w[Justin\ Knowlden]
|
10
|
+
s.post_install_message = %q{Choosy mailpersons choose Thumble Monks.}
|
11
|
+
|
12
|
+
s.rubyforge_project = %q{load_model}
|
13
|
+
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Load Model", "--main", "README.markdown"]
|
16
|
+
s.extra_rdoc_files = ["HISTORY.markdown", "README.markdown"]
|
17
|
+
|
18
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to?(:required_rubygems_version=)
|
19
|
+
s.rubygems_version = "1.3.1"
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
# run git ls-files to get an updated list
|
23
|
+
s.files = %w[
|
24
|
+
HISTORY.markdown
|
25
|
+
LICENSE
|
26
|
+
README.markdown
|
27
|
+
lib/shoulda_action_mailer.rb
|
28
|
+
shoulda_action_mailer.gemspec ]
|
29
|
+
|
30
|
+
s.test_files = %w[
|
31
|
+
]
|
32
|
+
|
33
|
+
s.post_install_message = %q{Choosy free loaders choose Thumble Monks}
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thumblemonks-thumblemonks-shoulda_action_mailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Knowlden
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-26 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Shoulda macros and more assertions for Action Mailer and ActionMailer::TestCase; now includes tests for parts.
|
17
|
+
email:
|
18
|
+
- gus@gusg.us
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- HISTORY.markdown
|
25
|
+
- README.markdown
|
26
|
+
files:
|
27
|
+
- HISTORY.markdown
|
28
|
+
- LICENSE
|
29
|
+
- README.markdown
|
30
|
+
- lib/shoulda_action_mailer.rb
|
31
|
+
- shoulda_action_mailer.gemspec
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/thumblemonks/shoulda_action_mailer
|
34
|
+
licenses:
|
35
|
+
post_install_message: Choosy free loaders choose Thumble Monks
|
36
|
+
rdoc_options:
|
37
|
+
- --line-numbers
|
38
|
+
- --inline-source
|
39
|
+
- --title
|
40
|
+
- Load Model
|
41
|
+
- --main
|
42
|
+
- README.markdown
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "1.2"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: load_model
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: Shoulda macros and more assertions for Action Mailer and ActionMailer::TestCase; now includes tests for parts.
|
64
|
+
test_files: []
|
65
|
+
|