From 7ad4c7c8814dc04a2d70967d887d2035b4d71b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 12 Mar 2012 19:41:17 -0300 Subject: Do not use the attributes hash in the scaffold functional tests --- .../lib/rails/generators/test_unit/scaffold/scaffold_generator.rb | 8 ++++++++ .../generators/test_unit/scaffold/templates/functional_test.rb | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'railties/lib/rails/generators/test_unit') diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index f7e907a017..760917896f 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -8,10 +8,18 @@ module TestUnit 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 diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb index 9ec2e34545..e9fe27e504 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -4,6 +4,7 @@ require 'test_helper' 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 @@ -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 %>)) @@ -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 -- cgit v1.2.3 From 08db3d5af3ac6a36036083e80f7bb33e65cc9dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 12 Mar 2012 20:10:40 -0300 Subject: Use the attributes hash explicitly --- .../test_unit/scaffold/scaffold_generator.rb | 19 ++++++++++++++++--- .../test_unit/scaffold/templates/functional_test.rb | 5 ++--- 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'railties/lib/rails/generators/test_unit') diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index 760917896f..4c07baae36 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -17,9 +17,22 @@ module TestUnit 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 diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb index e9fe27e504..19894443d5 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -4,7 +4,6 @@ require 'test_helper' 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 -- cgit v1.2.3 From 7c00cde79f7973d168b1b6aaa396594447e39a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 13 Mar 2012 14:19:03 -0300 Subject: Refactor the test_unit scaffold generator to use the key_value method --- railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib/rails/generators/test_unit') diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index 4c07baae36..8e796aada9 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -26,7 +26,7 @@ module TestUnit accessible_attributes.map do |a| name = a.name - "#{name}: @#{singular_table_name}.#{name}" + key_value name, "@#{singular_table_name}.#{name}" end.sort.join(', ') end -- cgit v1.2.3 From 1ced5ca67bc553ebc4a8a1b3ba4776e882587600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 13 Mar 2012 23:10:32 -0300 Subject: Remove key_value helper now that master is 1.9 only and we always use the 1.9 hash syntax in the generators. --- .../rails/generators/test_unit/scaffold/scaffold_generator.rb | 6 +----- .../generators/test_unit/scaffold/templates/functional_test.rb | 10 +++++----- 2 files changed, 6 insertions(+), 10 deletions(-) (limited to 'railties/lib/rails/generators/test_unit') diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index 8e796aada9..37b9f7ef7d 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -17,16 +17,12 @@ module TestUnit 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}" + "#{name}: @#{singular_table_name}.#{name}" end.sort.join(', ') end diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb index 19894443d5..30e1650555 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -19,30 +19,30 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post :create, <%= resource_attributes %> + post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %> end assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end test "should show <%= singular_table_name %>" do - get :show, <%= key_value :id, "@#{singular_table_name}" %> + get :show, id: <%= "@#{singular_table_name}" %> assert_response :success end test "should get edit" do - get :edit, <%= key_value :id, "@#{singular_table_name}" %> + get :edit, id: <%= "@#{singular_table_name}" %> assert_response :success end test "should update <%= singular_table_name %>" do - put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= resource_attributes %> + put :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end test "should destroy <%= singular_table_name %>" do assert_difference('<%= class_name %>.count', -1) do - delete :destroy, <%= key_value :id, "@#{singular_table_name}" %> + delete :destroy, id: <%= "@#{singular_table_name}" %> end assert_redirected_to <%= index_helper %>_path -- cgit v1.2.3 From 570cc89bad00a0df20e11196fa34d3119327a7d6 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 13 Mar 2012 10:03:59 -0300 Subject: Generate special controller and functional test templates for http apps The main goal is to not generate the format.html block in scaffold controller, and to generate a different functional test as we don't rely on redirects anymore, we should test for http responses. In addition to that, the :edit action is removed from the http controller and the edit route is not generated by default, as they usually do not make sense in this scenario. [Carlos Antonio da Silva & Santiago Pastorino] --- .../test_unit/scaffold/scaffold_generator.rb | 7 ++- .../scaffold/templates/http_functional_test.rb | 50 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb (limited to 'railties/lib/rails/generators/test_unit') diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index 37b9f7ef7d..e875c81340 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -10,8 +10,13 @@ module TestUnit argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" + class_option :http, :type => :boolean, :default => false, + :desc => "Generate functional test with HTTP actions only" + def create_test_files - template 'functional_test.rb', + template_file = options.http? ? "http_functional_test.rb" : "functional_test.rb" + + template template_file, File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb") end diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb new file mode 100644 index 0000000000..5bb61cb263 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb @@ -0,0 +1,50 @@ +require 'test_helper' + +<% module_namespacing do -%> +class <%= controller_class_name %>ControllerTest < ActionController::TestCase + setup do + @<%= singular_table_name %> = <%= table_name %>(:one) + @request.accept = "application/json" + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:<%= table_name %>) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create <%= singular_table_name %>" do + assert_difference('<%= class_name %>.count') do + post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %> + end + + assert_response 201 + assert_not_nil assigns(:<%= singular_table_name %>) + end + + test "should show <%= singular_table_name %>" do + get :show, id: @<%= singular_table_name %> + assert_response :success + end + + test "should update <%= singular_table_name %>" do + put :update, id: @<%= singular_table_name %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> + assert_response 204 + assert_not_nil assigns(:<%= singular_table_name %>) + end + + test "should destroy <%= singular_table_name %>" do + assert_difference('<%= class_name %>.count', -1) do + delete :destroy, id: @<%= singular_table_name %> + end + + assert_response 204 + assert_not_nil assigns(:<%= singular_table_name %>) + end +end +<% end -%> -- cgit v1.2.3 From 6db930cb5bbff9ad824590b5844e04768de240b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 14 Mar 2012 22:30:01 +0100 Subject: Remove --http. --- .../test_unit/scaffold/scaffold_generator.rb | 6 +-- .../scaffold/templates/http_functional_test.rb | 50 ---------------------- 2 files changed, 2 insertions(+), 54 deletions(-) delete mode 100644 railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb (limited to 'railties/lib/rails/generators/test_unit') diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index e875c81340..9e76587a0d 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -14,10 +14,8 @@ module TestUnit :desc => "Generate functional test with HTTP actions only" def create_test_files - template_file = options.http? ? "http_functional_test.rb" : "functional_test.rb" - - template template_file, - File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb") + template "functional_test.rb", + File.join("test/functional", controller_class_path, "#{controller_file_name}_controller_test.rb") end private diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb deleted file mode 100644 index 5bb61cb263..0000000000 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'test_helper' - -<% module_namespacing do -%> -class <%= controller_class_name %>ControllerTest < ActionController::TestCase - setup do - @<%= singular_table_name %> = <%= table_name %>(:one) - @request.accept = "application/json" - end - - test "should get index" do - get :index - assert_response :success - assert_not_nil assigns(:<%= table_name %>) - end - - test "should get new" do - get :new - assert_response :success - end - - test "should create <%= singular_table_name %>" do - assert_difference('<%= class_name %>.count') do - post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %> - end - - assert_response 201 - assert_not_nil assigns(:<%= singular_table_name %>) - end - - test "should show <%= singular_table_name %>" do - get :show, id: @<%= singular_table_name %> - assert_response :success - end - - test "should update <%= singular_table_name %>" do - put :update, id: @<%= singular_table_name %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> - assert_response 204 - assert_not_nil assigns(:<%= singular_table_name %>) - end - - test "should destroy <%= singular_table_name %>" do - assert_difference('<%= class_name %>.count', -1) do - delete :destroy, id: @<%= singular_table_name %> - end - - assert_response 204 - assert_not_nil assigns(:<%= singular_table_name %>) - end -end -<% end -%> -- cgit v1.2.3