Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[3-2-stable] Do not use the attributes hash in the scaffold functional tests #5410

Merged
merged 3 commits into from

2 participants

@rafaelfranca

No description provided.

...s/generators/test_unit/scaffold/scaffold_generator.rb
((6 lines not shown))
def create_test_files
template 'functional_test.rb',
File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb")
end
+
+ private
+
+ def resource_attributes
+ key_value singular_table_name, "{ #{attributes_hash} }"
+ end
+
+ def attributes_hash
+ return if accessible_attributes.empty?
+
+ accessible_attributes.map do |a|
+ name = a.name
+ "#{name}: @#{singular_table_name}.#{name}"
@josevalim Owner

We need to use 1.8 hash syntax.

@rafaelfranca Owner

I forget about this =/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@josevalim josevalim merged commit dfbbf31 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
21 railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
@@ -8,10 +8,31 @@ class ScaffoldGenerator < Base
check_class_collision :suffix => "ControllerTest"
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
def create_test_files
template 'functional_test.rb',
File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb")
end
+
+ private
+
+ def resource_attributes
+ key_value singular_table_name, "{ #{attributes_hash} }"
+ end
+
+ def attributes_hash
+ return if accessible_attributes.empty?
+
+ accessible_attributes.map do |a|
+ name = a.name
+ key_value name, "@#{singular_table_name}.#{name}"
+ end.sort.join(', ')
+ end
+
+ def accessible_attributes
+ attributes.reject(&:reference?)
+ end
end
end
end
View
4 railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb
@@ -19,7 +19,7 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
test "should create <%= singular_table_name %>" do
assert_difference('<%= class_name %>.count') do
- post :create, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %>
+ post :create, <%= resource_attributes %>
end
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
@@ -36,7 +36,7 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
end
test "should update <%= singular_table_name %>" do
- put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %>
+ put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= resource_attributes %>
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
end
View
25 railties/test/generators/scaffold_controller_generator_test.rb
@@ -75,6 +75,31 @@ def test_functional_tests
assert_file "test/functional/users_controller_test.rb" do |content|
assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
assert_match(/test "should get index"/, content)
+
+ if RUBY_VERSION < "1.9"
+ assert_match(/post :create, :user => \{ :age => @user.age, :name => @user.name \}/, content)
+ assert_match(/put :update, :id => @user, :user => \{ :age => @user.age, :name => @user.name \}/, content)
+ else
+ assert_match(/post :create, user: \{ age: @user.age, name: @user.name \}/, content)
+ assert_match(/put :update, id: @user, user: \{ age: @user.age, name: @user.name \}/, content)
+ end
+ end
+ end
+
+ def test_functional_tests_without_attributes
+ run_generator ["User"]
+
+ assert_file "test/functional/users_controller_test.rb" do |content|
+ assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
+ assert_match(/test "should get index"/, content)
+
+ if RUBY_VERSION < "1.9"
+ assert_match(/post :create, :user => \{ \}/, content)
+ assert_match(/put :update, :id => @user, :user => \{ \}/, content)
+ else
+ assert_match(/post :create, user: \{ \}/, content)
+ assert_match(/put :update, id: @user, user: \{ \}/, content)
+ end
end
end
View
30 railties/test/generators/scaffold_generator_test.rb
@@ -62,8 +62,17 @@ def test_scaffold_on_invoke
end
end
- assert_file "test/functional/product_lines_controller_test.rb",
- /class ProductLinesControllerTest < ActionController::TestCase/
+ assert_file "test/functional/product_lines_controller_test.rb" do |test|
+ assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test)
+
+ if RUBY_VERSION < "1.9"
+ assert_match(/post :create, :product_line => \{ :title => @product_line.title \}/, test)
+ assert_match(/put :update, :id => @product_line, :product_line => \{ :title => @product_line.title \}/, test)
+ else
+ assert_match(/post :create, product_line: \{ title: @product_line.title \}/, test)
+ assert_match(/put :update, id: @product_line, product_line: \{ title: @product_line.title \}/, test)
+ end
+ end
# Views
%w(
@@ -85,6 +94,23 @@ def test_scaffold_on_invoke
assert_file "app/assets/stylesheets/product_lines.css"
end
+ def test_functional_tests_without_attributes
+ run_generator ["product_line"]
+
+ assert_file "test/functional/product_lines_controller_test.rb" do |content|
+ assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, content)
+ assert_match(/test "should get index"/, content)
+
+ if RUBY_VERSION < "1.9"
+ assert_match(/post :create, :product_line => \{ \}/, content)
+ assert_match(/put :update, :id => @product_line, :product_line => \{ \}/, content)
+ else
+ assert_match(/post :create, product_line: \{ \}/, content)
+ assert_match(/put :update, id: @product_line, product_line: \{ \}/, content)
+ end
+ end
+ end
+
def test_scaffold_on_revoke
run_generator
run_generator ["product_line"], :behavior => :revoke
Something went wrong with that request. Please try again.