rails--rails/railties/test/generators/scaffold_controller_generat...

146 lines
4.2 KiB
Ruby
Raw Normal View History

2009-07-01 18:12:29 +00:00
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
2009-07-01 18:12:29 +00:00
module Unknown
module Generators
end
end
2009-07-01 18:12:29 +00:00
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
2010-01-03 14:13:54 +00:00
assert_instance_method :index, content do |m|
2009-07-01 18:12:29 +00:00
assert_match /@users = User\.all/, m
end
2010-01-03 14:13:54 +00:00
assert_instance_method :show, content do |m|
2009-07-01 18:12:29 +00:00
assert_match /@user = User\.find\(params\[:id\]\)/, m
end
2010-01-03 14:13:54 +00:00
assert_instance_method :new, content do |m|
2009-07-01 18:12:29 +00:00
assert_match /@user = User\.new/, m
end
2010-01-03 14:13:54 +00:00
assert_instance_method :edit, content do |m|
2009-07-01 18:12:29 +00:00
assert_match /@user = User\.find\(params\[:id\]\)/, m
end
2010-01-03 14:13:54 +00:00
assert_instance_method :create, content do |m|
2009-07-01 18:12:29 +00:00
assert_match /@user = User\.new\(params\[:user\]\)/, m
assert_match /@user\.save/, m
assert_match /@user\.errors/, m
end
2010-01-03 14:13:54 +00:00
assert_instance_method :update, content do |m|
2009-07-01 18:12:29 +00:00
assert_match /@user = User\.find\(params\[:id\]\)/, m
assert_match /@user\.update_attributes\(params\[:user\]\)/, m
assert_match /@user\.errors/, m
end
2010-01-03 14:13:54 +00:00
assert_instance_method :destroy, content do |m|
2009-07-01 18:12:29 +00:00
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_default_orm_is_used
run_generator ["User", "--orm=unknown"]
assert_file "app/controllers/users_controller.rb" do |content|
assert_match /class UsersController < ApplicationController/, content
2010-01-03 14:13:54 +00:00
assert_instance_method :index, content do |m|
assert_match /@users = User\.all/, m
end
end
end
def test_customized_orm_is_used
klass = Class.new(Rails::Generators::ActiveModel) do
def self.all(klass)
"#{klass}.find(:all)"
end
end
Unknown::Generators.const_set(:ActiveModel, klass)
run_generator ["User", "--orm=unknown"]
assert_file "app/controllers/users_controller.rb" do |content|
assert_match /class UsersController < ApplicationController/, content
2010-01-03 14:13:54 +00:00
assert_instance_method :index, content do |m|
assert_match /@users = User\.find\(:all\)/, m
assert_no_match /@users = User\.all/, m
end
end
ensure
Unknown::Generators.send :remove_const, :ActiveModel
2009-07-01 18:12:29 +00:00
end
protected
def run_generator(args=["User", "name:string", "age:integer"])
silence(:stdout) { Rails::Generators::ScaffoldControllerGenerator.start args, :destination_root => destination_root }
2009-07-01 18:12:29 +00:00
end
end