Added tests to scaffold controller.

This commit is contained in:
José Valim 2009-07-01 20:12:29 +02:00
parent 86ff074101
commit 1a4d8aca8a
9 changed files with 142 additions and 13 deletions

@ -7,6 +7,7 @@ module Generators
:fixture => true,
:force_plural => false,
:helper => true,
:layout => true,
:migration => true,
:orm => 'active_record',
:resource_controller => 'controller',

@ -6,9 +6,10 @@ class ScaffoldGenerator < Base
include Rails::Generators::ControllerNamedBase
argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type"
class_option :singleton, :type => :boolean, :desc => "Supply to skip index action"
# TODO Spec me
class_option :singleton, :type => :boolean, :desc => "Supply to skip index action"
class_option :layout, :type => :boolean
def copy_index_file
return if options[:singleton]
copy_view :index
@ -26,8 +27,8 @@ def copy_new_file
copy_view :new
end
# TODO invoke_if?
def copy_layout_file
return unless options[:layout]
template "layout.html.erb",
File.join("app/views/layouts", controller_class_path, "#{controller_file_name}.html.erb")
end

@ -1,10 +1,10 @@
<% for attribute in attributes -%>
<p>
<b><%= attribute.column.human_name %>:</b>
<b><%= attribute.human_name %>:</b>
<%%=h @<%= singular_name %>.<%= attribute.name %> %>
</p>
<% end -%>
<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
<%%= link_to 'Back', <%= plural_name %>_path %>
<%%= link_to 'Back', <%= plural_name %>_path %>

@ -3,8 +3,6 @@
module Rails
module Generators
class ScaffoldGenerator < ResourceGenerator #metagenerator
class_option :test_framework, :banner => "NAME", :desc => "Test framework to be invoked"
remove_hook_for :actions, :resource_controller
hook_for :scaffold_controller, :required => true

@ -4,11 +4,14 @@ class ScaffoldControllerGenerator < NamedBase
include ControllerNamedBase
check_class_collision :suffix => "Controller"
class_option :orm, :desc => "ORM to generate the controller for", :banner => "NAME", :type => :string
class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller" # TODO Spec me
class_option :orm, :banner => "NAME", :type => :string, :required => true,
:desc => "ORM to generate the controller for"
class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
def create_controller_files
template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb")
template 'controller.rb', File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
end
hook_for :template_engine, :test_framework, :as => :scaffold
@ -22,7 +25,12 @@ def create_controller_files
protected
def orm_class
@orm_class ||= "#{options[:orm].to_s.classify}::Generators::ActionORM".constantize
@orm_class ||= begin
action_orm = "#{options[:orm].to_s.classify}::Generators::ActionORM"
action_orm.constantize
rescue NameError => e
raise Error, "Could not load #{action_orm}, skipping controller. Error: #{e.message}."
end
end
def orm_instance

@ -4,6 +4,8 @@ module TestUnit
module Generators
class ScaffoldGenerator < Base
include Rails::Generators::ControllerNamedBase
class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
check_class_collision :suffix => "ControllerTest"
def create_test_files

@ -1,11 +1,13 @@
require 'test_helper'
class <%= controller_class_name %>ControllerTest < ActionController::TestCase
<% unless options[:singleton] -%>
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:<%= table_name %>)
end
<% end -%>
test "should get new" do
get :new

@ -72,8 +72,8 @@ def assert_class_method(content, method, &block)
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

@ -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