@@ -9,8 +9,8 @@ class MicrocosmLinksControllerTest < ActionDispatch::IntegrationTest
9
9
10
10
def test_routes
11
11
assert_routing (
12
- { :path => "/microcosm_links" , :method => :get } ,
13
- { :controller => "microcosm_links" , :action => "index" }
12
+ { :path => "/microcosms/foo/ microcosm_links" , :method => :get } ,
13
+ { :controller => "microcosm_links" , :action => "index" , :microcosm_id => "foo" }
14
14
)
15
15
assert_routing (
16
16
{ :path => "/microcosm_links/1/edit" , :method => :get } ,
@@ -21,12 +21,12 @@ def test_routes
21
21
{ :controller => "microcosm_links" , :action => "update" , :id => "1" }
22
22
)
23
23
assert_routing (
24
- { :path => "/microcosm_links/new" , :method => :get } ,
25
- { :controller => "microcosm_links" , :action => "new" }
24
+ { :path => "/microcosms/foo/ microcosm_links/new" , :method => :get } ,
25
+ { :controller => "microcosm_links" , :action => "new" , :microcosm_id => "foo" }
26
26
)
27
27
assert_routing (
28
- { :path => "/microcosm_links" , :method => :post } ,
29
- { :controller => "microcosm_links" , :action => "create" }
28
+ { :path => "/microcosms/foo/ microcosm_links" , :method => :post } ,
29
+ { :controller => "microcosm_links" , :action => "create" , :microcosm_id => "foo" }
30
30
)
31
31
end
32
32
@@ -35,7 +35,7 @@ def test_index_get
35
35
m = create ( :microcosm )
36
36
link = create ( :microcosm_link , :microcosm_id => m . id )
37
37
# act
38
- get microcosm_links_path ( :params => { :microcosm_id => m . id } )
38
+ get microcosm_microcosm_links_path ( m . id )
39
39
# assert
40
40
assert_response :success
41
41
assert_template "index"
@@ -58,7 +58,7 @@ def test_update_as_non_organizer
58
58
link = create ( :microcosm_link )
59
59
session_for ( create ( :user ) )
60
60
# act
61
- put microcosm_link_path ( link ) , :params => { : microcosm_link => link . as_json } , :xhr => true
61
+ put microcosm_link_path link , :microcosm_link => link
62
62
# assert
63
63
assert_redirected_to :controller => :errors , :action => :forbidden
64
64
end
@@ -120,11 +120,13 @@ def controller_mock.render(_partial)
120
120
def test_new_no_login
121
121
# Make sure that you are redirected to the login page when you
122
122
# are not logged in
123
+ # arrange
124
+ m = create ( :microcosm )
123
125
# act
124
- get new_microcosm_link_path
126
+ get new_microcosm_microcosm_link_path ( m )
125
127
# assert
126
128
assert_response :redirect
127
- assert_redirected_to login_path ( :referer => new_microcosm_link_path )
129
+ assert_redirected_to login_path ( :referer => new_microcosm_microcosm_link_path ( m ) )
128
130
end
129
131
130
132
def test_new_form
@@ -133,35 +135,39 @@ def test_new_form
133
135
m = create ( :microcosm )
134
136
session_for ( m . organizer )
135
137
# act
136
- get new_microcosm_link_path ( :params => { :microcosm_id => m . id } )
138
+ get new_microcosm_microcosm_link_path ( m )
137
139
# assert
138
140
assert_response :success
139
141
assert_select "div.content-heading" , :count => 1 do
140
142
assert_select "h1" , :text => /Microcosm Link/ , :count => 1
141
143
end
144
+ action = microcosm_microcosm_links_path ( m )
142
145
assert_select "div#content" , :count => 1 do
143
- assert_select "form[action='/microcosm_links '][method=post]" , :count => 1 do
146
+ assert_select "form[action='#{ action } '][method=post]" , :count => 1 do
144
147
assert_select "input#microcosm_link_site[name='microcosm_link[site]']" , :count => 1
145
148
assert_select "input#microcosm_link_url[name='microcosm_link[url]']" , :count => 1
146
- assert_select "input" , :count => 5
149
+ assert_select "input" , :count => 4
147
150
end
148
151
end
149
152
end
150
153
151
154
def test_create_when_save_works
152
155
# arrange
153
156
m = create ( :microcosm )
154
- link_orig = create ( :microcosm_link , :microcosm_id => m . id )
157
+ link_orig = create ( :microcosm_link , :microcosm => m )
158
+ form = link_orig . attributes . except ( "id" , "created_at" , "updated_at" )
155
159
session_for ( m . organizer )
156
160
157
161
# act
158
162
link_new_id = nil
159
163
assert_difference "MicrocosmLink.count" , 1 do
160
- post microcosm_links_url , :params => { :microcosm_link => link_orig . as_json } , :xhr => true
164
+ post microcosm_microcosm_links_path m . id , :microcosm_link => form
161
165
link_new_id = @response . headers [ "link_id" ]
162
166
end
163
167
164
168
# assert
169
+ # Not sure what's going on with this assigns magic.
170
+ # assert_redirected_to "/microcosm/#{assigns(:microcosm_link).id}"
165
171
assert_equal I18n . t ( "microcosm_links.create.success" ) , flash [ :notice ]
166
172
link_new = MicrocosmLink . find_by ( :id => link_new_id )
167
173
# Assign the id m_new to m_orig, so we can do an equality test easily.
@@ -171,29 +177,17 @@ def test_create_when_save_works
171
177
172
178
def test_create_when_save_fails
173
179
# arrange
174
- session_for ( create ( :user ) )
175
- link = create ( :microcosm_link )
176
- link = link . attributes . except ( "id" , "created_at" , "updated_at" , "slug" )
177
-
178
- mock_microcosm_link = Minitest ::Mock . new
179
- mock_microcosm_link . expect :save , false
180
-
181
- # We're going to stub render on this instance.
182
- controller_prime = MicrocosmLinksController . new
180
+ mic = create ( :microcosm )
181
+ session_for ( mic . organizer )
182
+ link = build ( :microcosm_link , :microcosm => mic , :url => "invalid url" )
183
+ form = link . attributes . except ( "id" , "created_at" , "updated_at" )
183
184
184
185
# act and assert
185
- MicrocosmLinksController . stub :new , controller_prime do
186
- controller_prime . stub :render , "" do
187
- MicrocosmLink . stub :new , mock_microcosm_link do
188
- assert_difference "MicrocosmLink.count" , 0 do
189
- post microcosm_links_path , :params => { :microcosm_link => link . as_json } , :xhr => true
190
- end
191
- end
192
- end
186
+ assert_no_difference "MicrocosmLink.count" , 0 do
187
+ post microcosm_microcosm_links_path :microcosm_link => form , :microcosm_id => mic . id
193
188
end
194
189
195
- # assert_mock mock_microcosm
196
- # assert_mock render_mock
190
+ assert_template :new
197
191
end
198
192
199
193
def test_delete
0 commit comments