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/generators/resource_generator_test.rb | 6 ++++ .../scaffold_controller_generator_test.rb | 37 ++++++++++++++++++++++ railties/test/generators_test.rb | 11 ++++++- 3 files changed, 53 insertions(+), 1 deletion(-) (limited to 'railties/test') diff --git a/railties/test/generators/resource_generator_test.rb b/railties/test/generators/resource_generator_test.rb index 73804dae45..4ba3ad8c41 100644 --- a/railties/test/generators/resource_generator_test.rb +++ b/railties/test/generators/resource_generator_test.rb @@ -86,4 +86,10 @@ class ResourceGeneratorTest < Rails::Generators::TestCase assert_no_match(/resources :accounts$/, route) end end + + def test_http_only_does_not_generate_edit_route + run_generator ["Account", "--http"] + + assert_file "config/routes.rb", /resources :accounts, except: :edit$/ + end end diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 1eea50b0d9..ed54ce43da 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -142,4 +142,41 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_match(/\{ render action: "new" \}/, content) end end + + def test_http_only_generates_controller_without_respond_to_block_and_html_format + run_generator ["User", "--http"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/render json: @user/, content) + assert_no_match(/respond_to/, content) + assert_no_match(/format\.html/, content) + end + end + + def test_http_only_generates_functional_tests_with_json_format_and_http_status_assertions + run_generator ["User", "--http"] + + assert_file "test/functional/users_controller_test.rb" do |content| + assert_match(/class UsersControllerTest < ActionController::TestCase/, content) + assert_match(/@request\.accept = "application\/json"/, content) + assert_match(/test "should get index"/, content) + + assert_match(/assert_response 201/, content) + assert_no_match(/assert_redirected_to/, content) + end + end + + def test_http_only_does_not_generate_edit_action + run_generator ["User", "--http"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/def index/, content) + assert_no_match(/def edit/, content) + end + + assert_file "test/functional/users_controller_test.rb" do |content| + assert_match(/test "should get index"/, content) + assert_no_match(/test "should get edit"/, content) + end + end end diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index eae34da9c0..9e7ec86fdf 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -229,6 +229,16 @@ class GeneratorsTest < Rails::Generators::TestCase end end + def test_http_only_enables_http_option + options = Rails::Generators.options[:rails] + + assert !options[:http], "http option should be disabled by default" + + with_http_only! do + assert options[:http], "http only should enable generator http option" + end + end + def test_http_only_disables_template_and_helper_and_assets_options options = Rails::Generators.options[:rails] disable_options = [:assets, :helper, :javascripts, :javascript_engine, @@ -251,7 +261,6 @@ class GeneratorsTest < Rails::Generators::TestCase Rails::Generators.http_only! yield ensure - Rails::Generators.instance_variable_set(:@http_only, false) Rails::Generators.instance_variable_set(:@hidden_namespaces, nil) Rails::Generators.instance_variable_set(:@options, nil) end -- cgit v1.2.3