Skip to content

Commit 21b69b2

Browse files
committed
Merge pull request rails#5397 from rafaelfranca/fix-scaffold
Do not use the attributes hash in the scaffold functional tests
2 parents e7d55f7 + 08db3d5 commit 21b69b2

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,31 @@ class ScaffoldGenerator < Base
88

99
check_class_collision :suffix => "ControllerTest"
1010

11+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
12+
1113
def create_test_files
1214
template 'functional_test.rb',
1315
File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb")
1416
end
17+
18+
private
19+
20+
def resource_attributes
21+
key_value singular_table_name, "{ #{attributes_hash} }"
22+
end
23+
24+
def attributes_hash
25+
return if accessible_attributes.empty?
26+
27+
accessible_attributes.map do |a|
28+
name = a.name
29+
"#{name}: @#{singular_table_name}.#{name}"
30+
end.sort.join(', ')
31+
end
32+
33+
def accessible_attributes
34+
attributes.reject(&:reference?)
35+
end
1536
end
1637
end
1738
end

railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
1919
2020
test "should create <%= singular_table_name %>" do
2121
assert_difference('<%= class_name %>.count') do
22-
post :create, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %>
22+
post :create, <%= resource_attributes %>
2323
end
2424
2525
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
@@ -36,7 +36,7 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
3636
end
3737
3838
test "should update <%= singular_table_name %>" do
39-
put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %>
39+
put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= resource_attributes %>
4040
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
4141
end
4242

railties/test/generators/scaffold_controller_generator_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ def test_functional_tests
7575
assert_file "test/functional/users_controller_test.rb" do |content|
7676
assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
7777
assert_match(/test "should get index"/, content)
78+
assert_match(/post :create, user: { age: @user.age, name: @user.name }/, content)
79+
assert_match(/put :update, id: @user, user: { age: @user.age, name: @user.name }/, content)
80+
end
81+
end
82+
83+
def test_functional_tests_without_attributes
84+
run_generator ["User"]
85+
86+
assert_file "test/functional/users_controller_test.rb" do |content|
87+
assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
88+
assert_match(/test "should get index"/, content)
89+
assert_match(/post :create, user: { }/, content)
90+
assert_match(/put :update, id: @user, user: { }/, content)
7891
end
7992
end
8093

railties/test/generators/scaffold_generator_test.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ def test_scaffold_on_invoke
6262
end
6363
end
6464

65-
assert_file "test/functional/product_lines_controller_test.rb",
66-
/class ProductLinesControllerTest < ActionController::TestCase/
65+
assert_file "test/functional/product_lines_controller_test.rb" do |test|
66+
assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test)
67+
assert_match(/post :create, product_line: { title: @product_line.title }/, test)
68+
assert_match(/put :update, id: @product_line, product_line: { title: @product_line.title }/, test)
69+
end
6770

6871
# Views
6972
%w(
@@ -85,6 +88,17 @@ def test_scaffold_on_invoke
8588
assert_file "app/assets/stylesheets/product_lines.css"
8689
end
8790

91+
def test_functional_tests_without_attributes
92+
run_generator ["product_line"]
93+
94+
assert_file "test/functional/product_lines_controller_test.rb" do |content|
95+
assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, content)
96+
assert_match(/test "should get index"/, content)
97+
assert_match(/post :create, product_line: { }/, content)
98+
assert_match(/put :update, id: @product_line, product_line: { }/, content)
99+
end
100+
end
101+
88102
def test_scaffold_on_revoke
89103
run_generator
90104
run_generator ["product_line"], :behavior => :revoke

0 commit comments

Comments
 (0)