Permalink
Browse files

Use the attributes hash explicitly

  • Loading branch information...
1 parent 7ad4c7c commit 08db3d5af3ac6a36036083e80f7bb33e65cc9dd3 @rafaelfranca rafaelfranca committed Mar 12, 2012
@@ -17,9 +17,22 @@ def create_test_files
private
- def accessible_attributes
- attributes.reject(&:reference?).map {|a| "\"#{a.name}\"" }.sort.join(', ')
- end
+ 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
@@ -4,7 +4,6 @@
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
@@ -20,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, "@valid_attributes" %>
+ post :create, <%= resource_attributes %>
end
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
@@ -37,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, "@valid_attributes" %>
+ put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= resource_attributes %>
assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
end
@@ -75,9 +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(/@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)
+ 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
@@ -64,9 +64,8 @@ def test_scaffold_on_invoke
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)
+ 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
@@ -89,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

0 comments on commit 08db3d5

Please sign in to comment.