1 require 'abstract_unit'
6 def new_record
?; @id.nil? end
8 model
= self.class.name
.downcase
9 @id.nil? ? "new #{model}" : "#{model} ##{@id}"
13 class Response
< Article
18 def response_id
; 1 end
21 # TODO: test nested models
22 class Response
::Nested < Response
; end
24 uses_mocha
'polymorphic URL helpers' do
25 class PolymorphicRoutesTest
< Test
::Unit::TestCase
27 include ActionController
::PolymorphicRoutes
30 @article = Article
.new
31 @response = Response
.new
36 expects(:article_url).with(@article)
37 polymorphic_url(@article)
40 def test_with_new_record
41 expects(:articles_url).with()
42 @article.expects(:new_record?).returns(true)
43 polymorphic_url(@article)
46 def test_with_record_and_action
47 expects(:new_article_url).with()
48 @article.expects(:new_record?).never
49 polymorphic_url(@article, :action => 'new')
52 def test_url_helper_prefixed_with_new
53 expects(:new_article_url).with()
54 new_polymorphic_url(@article)
57 def test_url_helper_prefixed_with_edit
59 expects(:edit_article_url).with(@article)
60 edit_polymorphic_url(@article)
63 def test_url_helper_prefixed_with_edit_with_url_options
65 expects(:edit_article_url).with(@article, :param1 => '10')
66 edit_polymorphic_url(@article, :param1 => '10')
69 def test_url_helper_with_url_options
71 expects(:article_url).with(@article, :param1 => '10')
72 polymorphic_url(@article, :param1 => '10')
75 def test_formatted_url_helper
76 expects(:formatted_article_url).with(@article, :pdf)
77 formatted_polymorphic_url([@article, :pdf])
80 def test_format_option
82 expects(:formatted_article_url).with(@article, :pdf)
83 polymorphic_url(@article, :format => :pdf)
86 def test_format_option_with_url_options
88 expects(:formatted_article_url).with(@article, :pdf, :param1 => '10')
89 polymorphic_url(@article, :format => :pdf, :param1 => '10')
92 def test_id_and_format_option
94 expects(:article_url).with(:id => @article, :format => :pdf)
95 polymorphic_url(:id => @article, :format => :pdf)
100 expects(:article_response_url).with(@article, @response)
101 polymorphic_url([@article, @response])
104 def test_with_nested_unsaved
105 expects(:article_responses_url).with(@article)
106 polymorphic_url([@article, @response])
109 def test_new_with_array_and_namespace
110 expects(:new_admin_article_url).with()
111 polymorphic_url([:admin, @article], :action => 'new')
114 def test_unsaved_with_array_and_namespace
115 expects(:admin_articles_url).with()
116 polymorphic_url([:admin, @article])
119 def test_nested_unsaved_with_array_and_namespace
121 expects(:admin_article_url).with(@article)
122 polymorphic_url([:admin, @article])
123 expects(:admin_article_responses_url).with(@article)
124 polymorphic_url([:admin, @article, @response])
127 def test_nested_with_array_and_namespace
129 expects(:admin_article_response_url).with(@article, @response)
130 polymorphic_url([:admin, @article, @response])
132 # a ridiculously long named route tests correct ordering of namespaces and nesting:
135 expects(:site_admin_article_response_tag_url).with(@article, @response, @tag)
136 polymorphic_url([:site, :admin, @article, @response, @tag])
139 def test_nesting_with_array_ending_in_singleton_resource
140 expects(:article_response_url).with(@article)
141 polymorphic_url([@article, :response])
144 def test_nesting_with_array_containing_singleton_resource
147 expects(:article_response_tag_url).with(@article, @tag)
148 polymorphic_url([@article, :response, @tag])
151 def test_nesting_with_array_containing_namespace_and_singleton_resource
154 expects(:admin_article_response_tag_url).with(@article, @tag)
155 polymorphic_url([:admin, @article, :response, @tag])
158 def test_nesting_with_array_containing_singleton_resource_and_format
161 expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf)
162 formatted_polymorphic_url([@article, :response, @tag, :pdf])
165 def test_nesting_with_array_containing_singleton_resource_and_format_option
168 expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf)
169 polymorphic_url([@article, :response, @tag], :format => :pdf)
172 def test_nesting_with_array_containing_nil
173 expects(:article_response_url).with(@article)
174 polymorphic_url([@article, nil, :response])
177 def test_with_array_containing_single_object
179 expects(:article_url).with(@article)
180 polymorphic_url([nil, @article])
183 def test_with_array_containing_single_name
185 expects(:articles_url)
186 polymorphic_url([:articles])
189 # TODO: Needs to be updated to correctly know about whether the object is in a hash or not
191 expects(:article_url).with(@article)
193 polymorphic_url(:id => @article)
196 def test_polymorphic_path_accepts_options
197 expects(:new_article_path).with()
198 polymorphic_path(@article, :action => :new)
201 def test_polymorphic_path_does_not_modify_arguments
202 expects(:admin_article_responses_url).with(@article)
203 path
= [:admin, @article, @response]
204 assert_no_difference
'path.size' do
205 polymorphic_url(path
)