Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use the attributes hash in the scaffold functional tests #5397

Merged
merged 2 commits into from
Mar 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
"#{name}: @#{singular_table_name}.#{name}"
end.sort.join(', ')
end

def accessible_attributes
attributes.reject(&:reference?)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>))
Expand All @@ -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

Expand Down
13 changes: 13 additions & 0 deletions railties/test/generators/scaffold_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ 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)
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

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)
assert_match(/post :create, user: { }/, content)
assert_match(/put :update, id: @user, user: { }/, content)
end
end

Expand Down
18 changes: 16 additions & 2 deletions railties/test/generators/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ 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)
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

# Views
%w(
Expand All @@ -85,6 +88,17 @@ 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)
assert_match(/post :create, product_line: { }/, content)
assert_match(/put :update, id: @product_line, product_line: { }/, content)
end
end

def test_scaffold_on_revoke
run_generator
run_generator ["product_line"], :behavior => :revoke
Expand Down