@@ -23,9 +23,32 @@ def signed_up_with_url(recipient)
2323 mail ( to : recipient , subject : "[Signed up] Welcome #{ recipient } " ,
2424 from : "system@loudthinking.com" , date : Time . local ( 2004 , 12 , 12 ) )
2525 end
26+
27+ def exercise_url_for ( options )
28+ @options = options
29+ @url = url_for ( @options )
30+ mail ( from : 'from@example.com' , to : 'to@example.com' , subject : 'subject' )
31+ end
2632end
2733
2834class ActionMailerUrlTest < ActionMailer ::TestCase
35+ class DummyModel
36+ def self . model_name
37+ OpenStruct . new ( route_key : 'dummy_model' )
38+ end
39+
40+ def persisted?
41+ false
42+ end
43+
44+ def model_name
45+ self . class . model_name
46+ end
47+
48+ def to_model
49+ self
50+ end
51+ end
2952
3053 def encode ( text , charset = "UTF-8" )
3154 quoted_printable ( text , charset )
@@ -40,10 +63,47 @@ def new_mail( charset="UTF-8" )
4063 mail
4164 end
4265
66+ def assert_url_for ( expected , options , relative : false )
67+ expected = "http://www.basecamphq.com#{ expected } " if expected . start_with? ( '/' ) && !relative
68+ urls = UrlTestMailer . exercise_url_for ( options ) . body . to_s . chomp . split
69+
70+ assert_equal expected , urls . first
71+ assert_equal expected , urls . second
72+ end
73+
4374 def setup
4475 @recipient = 'test@localhost'
4576 end
4677
78+ def test_url_for
79+ UrlTestMailer . delivery_method = :test
80+
81+ AppRoutes . draw do
82+ get ':controller(/:action(/:id))'
83+ get '/welcome' => 'foo#bar' , as : 'welcome'
84+ get '/dummy_model' => 'foo#baz' , as : 'dummy_model'
85+ end
86+
87+ # string
88+ assert_url_for 'http://foo/' , 'http://foo/'
89+
90+ # symbol
91+ assert_url_for '/welcome' , :welcome
92+
93+ # hash
94+ assert_url_for '/a/b/c' , controller : 'a' , action : 'b' , id : 'c'
95+ assert_url_for '/a/b/c' , { controller : 'a' , action : 'b' , id : 'c' , only_path : true } , relative : true
96+
97+ # model
98+ assert_url_for '/dummy_model' , DummyModel . new
99+
100+ # class
101+ assert_url_for '/dummy_model' , DummyModel
102+
103+ # array
104+ assert_url_for '/dummy_model' , [ DummyModel ]
105+ end
106+
47107 def test_signed_up_with_url
48108 UrlTestMailer . delivery_method = :test
49109
0 commit comments