diff options
author | José Valim <jose.valim@gmail.com> | 2009-07-01 20:12:29 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-07-02 10:27:40 +0200 |
commit | 1a4d8aca8a675aa02cead4b1db53ba5a3f9aa326 (patch) | |
tree | 788c5de55db47408ee4e1376ba1cdc11d3923b5c /railties/test | |
parent | 86ff07410145430d08e97a8296486c4cd4304cde (diff) | |
download | rails-1a4d8aca8a675aa02cead4b1db53ba5a3f9aa326.tar.gz rails-1a4d8aca8a675aa02cead4b1db53ba5a3f9aa326.tar.bz2 rails-1a4d8aca8a675aa02cead4b1db53ba5a3f9aa326.zip |
Added tests to scaffold controller.
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/generators/generators_test_helper.rb | 4 | ||||
-rw-r--r-- | railties/test/generators/scaffold_controller_generator_test.rb | 117 |
2 files changed, 119 insertions, 2 deletions
diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index 591da45c72..578753e070 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -72,8 +72,8 @@ class GeneratorsTestCase < Test::Unit::TestCase end def assert_instance_method(content, method) - assert_match /def #{method}(.*?)end/m, content - yield content.match(/def #{method}(.*?)end/m)[1] if block_given? + assert content =~ /def #{method}(\(.+\))?(.*?)\n end/m, "Expected to have method #{method}" + yield $2.strip if block_given? end protected diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb new file mode 100644 index 0000000000..60c1145310 --- /dev/null +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -0,0 +1,117 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record' +require 'generators/erb/scaffold/scaffold_generator' +require 'generators/rails/helper/helper_generator' +require 'generators/rails/scaffold_controller/scaffold_controller_generator' +require 'generators/test_unit/scaffold/scaffold_generator' +require 'generators/test_unit/helper/helper_generator' + +class ScaffoldControllerGeneratorTest < GeneratorsTestCase + + def test_controller_skeleton_is_created + run_generator + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match /class UsersController < ApplicationController/, content + + assert_instance_method content, :index do |m| + assert_match /@users = User\.all/, m + end + + assert_instance_method content, :show do |m| + assert_match /@user = User\.find\(params\[:id\]\)/, m + end + + assert_instance_method content, :new do |m| + assert_match /@user = User\.new/, m + end + + assert_instance_method content, :edit do |m| + assert_match /@user = User\.find\(params\[:id\]\)/, m + end + + assert_instance_method content, :create do |m| + assert_match /@user = User\.new\(params\[:user\]\)/, m + assert_match /@user\.save/, m + assert_match /@user\.errors/, m + end + + assert_instance_method content, :update do |m| + assert_match /@user = User\.find\(params\[:id\]\)/, m + assert_match /@user\.update_attributes\(params\[:user\]\)/, m + assert_match /@user\.errors/, m + end + + assert_instance_method content, :destroy do |m| + assert_match /@user = User\.find\(params\[:id\]\)/, m + assert_match /@user\.destroy/, m + end + end + end + + def test_helper_are_invoked_with_a_pluralized_name + run_generator + assert_file "app/helpers/users_helper.rb", /module UsersHelper/ + assert_file "test/unit/helpers/users_helper_test.rb", /class UsersHelperTest < ActionView::TestCase/ + end + + def test_views_are_generated + run_generator + + %w( + index + edit + new + show + ).each { |view| assert_file "app/views/users/#{view}.html.erb" } + assert_file "app/views/layouts/users.html.erb" + end + + def test_functional_tests + run_generator + + assert_file "test/functional/users_controller_test.rb" do |content| + assert_match /class UsersControllerTest < ActionController::TestCase/, content + assert_match /test "should get index"/, content + end + end + + def test_generates_singleton_controller + run_generator ["User", "name:string", "age:integer", "--singleton"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_no_match /def index/, content + end + + assert_file "test/functional/users_controller_test.rb" do |content| + assert_no_match /test "should get index"/, content + end + + assert_no_file "app/views/users/index.html.erb" + end + + def test_skip_helper_if_required + run_generator ["User", "name:string", "age:integer", "--no-helper"] + assert_no_file "app/helpers/users_helper.rb" + assert_no_file "test/unit/helpers/users_helper_test.rb" + end + + def test_skip_layout_if_required + run_generator ["User", "name:string", "age:integer", "--no-layout"] + assert_no_file "app/views/layouts/users.html.erb" + end + + def test_error_is_shown_if_orm_does_not_provide_interface + error = capture(:stderr){ run_generator ["User", "--orm=unknown"] } + assert_equal "Could not load Unknown::Generators::ActionORM, skipping controller. " << + "Error: uninitialized constant Unknown.\n", error + end + + protected + + def run_generator(args=["User", "name:string", "age:integer"]) + silence(:stdout) { Rails::Generators::ScaffoldControllerGenerator.start args, :root => destination_root } + end + +end |