Skip to content

Commit

Permalink
Do not use the attributes hash in the scaffold functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Mar 12, 2012
1 parent 2ad34f4 commit 7ad4c7c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
Expand Up @@ -8,10 +8,18 @@ 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 accessible_attributes
attributes.reject(&:reference?).map {|a| "\"#{a.name}\"" }.sort.join(', ')
end
end
end
end
Expand Up @@ -4,6 +4,7 @@
class <%= controller_class_name %>ControllerTest < ActionController::TestCase
setup do
@<%= singular_table_name %> = <%= table_name %>(:one)
@valid_attributes = @<%= singular_table_name %>.attributes.slice(<%= accessible_attributes %>)
end

test "should get index" do
Expand All @@ -19,7 +20,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, <%= key_value singular_table_name, "@valid_attributes" %>
end
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
Expand All @@ -36,7 +37,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}" %>, <%= key_value singular_table_name, "@valid_attributes" %>
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
end

Expand Down
Expand Up @@ -75,6 +75,9 @@ 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(/@valid_attributes = @user\.attributes\.slice\("age", "name"\)/, content)
assert_match(/post :create, user: @valid_attributes/, content)
assert_match(/put :update, id: @user, user: @valid_attributes/, content)
end
end

Expand Down
8 changes: 6 additions & 2 deletions railties/test/generators/scaffold_generator_test.rb
Expand Up @@ -62,8 +62,12 @@ 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(/@valid_attributes = @product_line\.attributes\.slice\("title"\)/, test)
assert_match(/post :create, product_line: @valid_attributes/, test)
assert_match(/put :update, id: @product_line, product_line: @valid_attributes/, test)
end

# Views
%w(
Expand Down

0 comments on commit 7ad4c7c

Please sign in to comment.