2009-07-01 16:06:05 -04:00
|
|
|
require 'generators/generators_test_helper'
|
2010-03-23 08:40:19 -04:00
|
|
|
require 'rails/generators/rails/scaffold/scaffold_generator'
|
2009-07-01 16:06:05 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class ScaffoldGeneratorTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2010-12-16 17:10:30 -05:00
|
|
|
arguments %w(product_line title:string product:belongs_to user:references)
|
2009-07-01 16:06:05 -04:00
|
|
|
|
2010-03-19 13:11:37 -04:00
|
|
|
setup :copy_routes
|
2009-07-01 16:06:05 -04:00
|
|
|
|
|
|
|
def test_scaffold_on_invoke
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
# Model
|
|
|
|
assert_file "app/models/product_line.rb", /class ProductLine < ActiveRecord::Base/
|
|
|
|
assert_file "test/unit/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/
|
|
|
|
assert_file "test/fixtures/product_lines.yml"
|
2012-04-16 00:14:50 -04:00
|
|
|
assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product, index: true/
|
|
|
|
assert_migration "db/migrate/create_product_lines.rb", /references :user, index: true/
|
2009-07-01 16:06:05 -04:00
|
|
|
|
|
|
|
# Route
|
|
|
|
assert_file "config/routes.rb" do |route|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/resources :product_lines$/, route)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Controller
|
|
|
|
assert_file "app/controllers/product_lines_controller.rb" do |content|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/class ProductLinesController < ApplicationController/, content)
|
2009-07-01 16:06:05 -04:00
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :index, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@product_lines = ProductLine\.all/, m)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :show, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :new, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@product_line = ProductLine\.new/, m)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :edit, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :create, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@product_line = ProductLine\.new\(params\[:product_line\]\)/, m)
|
|
|
|
assert_match(/@product_line\.save/, m)
|
|
|
|
assert_match(/@product_line\.errors/, m)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :update, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
|
|
|
|
assert_match(/@product_line\.update_attributes\(params\[:product_line\]\)/, m)
|
|
|
|
assert_match(/@product_line\.errors/, m)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :destroy, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
|
|
|
|
assert_match(/@product_line\.destroy/, m)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-12 18:41:17 -04:00
|
|
|
assert_file "test/functional/product_lines_controller_test.rb" do |test|
|
|
|
|
assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test)
|
2012-03-13 13:19:03 -04:00
|
|
|
assert_match(/post :create, product_line: \{ title: @product_line.title \}/, test)
|
|
|
|
assert_match(/put :update, id: @product_line, product_line: \{ title: @product_line.title \}/, test)
|
2012-03-12 18:41:17 -04:00
|
|
|
end
|
2009-07-01 16:06:05 -04:00
|
|
|
|
|
|
|
# Views
|
|
|
|
%w(
|
|
|
|
index
|
|
|
|
edit
|
|
|
|
new
|
|
|
|
show
|
2009-08-30 12:49:54 -04:00
|
|
|
_form
|
2009-07-01 16:06:05 -04:00
|
|
|
).each { |view| assert_file "app/views/product_lines/#{view}.html.erb" }
|
2010-04-09 13:04:20 -04:00
|
|
|
assert_no_file "app/views/layouts/product_lines.html.erb"
|
2009-07-01 16:06:05 -04:00
|
|
|
|
|
|
|
# Helpers
|
|
|
|
assert_file "app/helpers/product_lines_helper.rb"
|
|
|
|
assert_file "test/unit/helpers/product_lines_helper_test.rb"
|
2010-04-09 13:04:20 -04:00
|
|
|
|
2011-04-17 05:44:52 -04:00
|
|
|
# Assets
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css"
|
2011-06-27 20:54:03 -04:00
|
|
|
assert_file "app/assets/javascripts/product_lines.js"
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/product_lines.css"
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
2012-03-12 19:10:40 -04:00
|
|
|
def test_functional_tests_without_attributes
|
|
|
|
run_generator ["product_line"]
|
|
|
|
|
|
|
|
assert_file "test/functional/product_lines_controller_test.rb" do |content|
|
|
|
|
assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, content)
|
|
|
|
assert_match(/test "should get index"/, content)
|
2012-03-13 13:19:03 -04:00
|
|
|
assert_match(/post :create, product_line: \{ \}/, content)
|
|
|
|
assert_match(/put :update, id: @product_line, product_line: \{ \}/, content)
|
2012-03-12 19:10:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-01 16:06:05 -04:00
|
|
|
def test_scaffold_on_revoke
|
|
|
|
run_generator
|
2010-01-03 10:34:32 -05:00
|
|
|
run_generator ["product_line"], :behavior => :revoke
|
2009-07-01 16:06:05 -04:00
|
|
|
|
|
|
|
# Model
|
|
|
|
assert_no_file "app/models/product_line.rb"
|
|
|
|
assert_no_file "test/unit/product_line_test.rb"
|
|
|
|
assert_no_file "test/fixtures/product_lines.yml"
|
|
|
|
assert_no_migration "db/migrate/create_product_lines.rb"
|
|
|
|
|
|
|
|
# Route
|
|
|
|
assert_file "config/routes.rb" do |route|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_no_match(/resources :product_lines$/, route)
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Controller
|
|
|
|
assert_no_file "app/controllers/product_lines_controller.rb"
|
|
|
|
assert_no_file "test/functional/product_lines_controller_test.rb"
|
|
|
|
|
|
|
|
# Views
|
|
|
|
assert_no_file "app/views/product_lines"
|
2010-04-09 13:04:20 -04:00
|
|
|
assert_no_file "app/views/layouts/product_lines.html.erb"
|
2009-07-01 16:06:05 -04:00
|
|
|
|
|
|
|
# Helpers
|
|
|
|
assert_no_file "app/helpers/product_lines_helper.rb"
|
|
|
|
assert_no_file "test/unit/helpers/product_lines_helper_test.rb"
|
2010-04-09 13:04:20 -04:00
|
|
|
|
2011-04-17 05:44:52 -04:00
|
|
|
# Assets
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css", /:visited/
|
2011-06-27 20:54:03 -04:00
|
|
|
assert_no_file "app/assets/javascripts/product_lines.js"
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_no_file "app/assets/stylesheets/product_lines.css"
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|
2010-06-23 03:03:34 -04:00
|
|
|
|
|
|
|
def test_scaffold_with_namespace_on_invoke
|
|
|
|
run_generator [ "admin/role", "name:string", "description:string" ]
|
|
|
|
|
|
|
|
# Model
|
|
|
|
assert_file "app/models/admin.rb", /module Admin/
|
|
|
|
assert_file "app/models/admin/role.rb", /class Admin::Role < ActiveRecord::Base/
|
|
|
|
assert_file "test/unit/admin/role_test.rb", /class Admin::RoleTest < ActiveSupport::TestCase/
|
|
|
|
assert_file "test/fixtures/admin/roles.yml"
|
|
|
|
assert_migration "db/migrate/create_admin_roles.rb"
|
|
|
|
|
|
|
|
# Route
|
|
|
|
assert_file "config/routes.rb" do |route|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/namespace :admin do resources :roles end$/, route)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Controller
|
|
|
|
assert_file "app/controllers/admin/roles_controller.rb" do |content|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/class Admin::RolesController < ApplicationController/, content)
|
2010-06-23 03:03:34 -04:00
|
|
|
|
|
|
|
assert_instance_method :index, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@admin_roles = Admin::Role\.all/, m)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_method :show, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_method :new, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@admin_role = Admin::Role\.new/, m)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_method :edit, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_method :create, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@admin_role = Admin::Role\.new\(params\[:admin_role\]\)/, m)
|
|
|
|
assert_match(/@admin_role\.save/, m)
|
|
|
|
assert_match(/@admin_role\.errors/, m)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_method :update, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m)
|
|
|
|
assert_match(/@admin_role\.update_attributes\(params\[:admin_role\]\)/, m)
|
|
|
|
assert_match(/@admin_role\.errors/, m)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_method :destroy, content do |m|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m)
|
|
|
|
assert_match(/@admin_role\.destroy/, m)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "test/functional/admin/roles_controller_test.rb",
|
|
|
|
/class Admin::RolesControllerTest < ActionController::TestCase/
|
|
|
|
|
|
|
|
# Views
|
|
|
|
%w(
|
|
|
|
index
|
|
|
|
edit
|
|
|
|
new
|
|
|
|
show
|
|
|
|
_form
|
|
|
|
).each { |view| assert_file "app/views/admin/roles/#{view}.html.erb" }
|
|
|
|
assert_no_file "app/views/layouts/admin/roles.html.erb"
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
assert_file "app/helpers/admin/roles_helper.rb"
|
|
|
|
assert_file "test/unit/helpers/admin/roles_helper_test.rb"
|
|
|
|
|
2011-04-17 05:44:52 -04:00
|
|
|
# Assets
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css", /:visited/
|
2011-06-27 20:54:03 -04:00
|
|
|
assert_file "app/assets/javascripts/admin/roles.js"
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/admin/roles.css"
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_scaffold_with_namespace_on_revoke
|
|
|
|
run_generator [ "admin/role", "name:string", "description:string" ]
|
|
|
|
run_generator [ "admin/role" ], :behavior => :revoke
|
|
|
|
|
|
|
|
# Model
|
|
|
|
assert_file "app/models/admin.rb" # ( should not be remove )
|
|
|
|
assert_no_file "app/models/admin/role.rb"
|
|
|
|
assert_no_file "test/unit/admin/role_test.rb"
|
|
|
|
assert_no_file "test/fixtures/admin/roles.yml"
|
|
|
|
assert_no_migration "db/migrate/create_admin_roles.rb"
|
|
|
|
|
|
|
|
# Route
|
|
|
|
assert_file "config/routes.rb" do |route|
|
2010-06-16 09:25:21 -04:00
|
|
|
assert_no_match(/namespace :admin do resources :roles end$/, route)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Controller
|
|
|
|
assert_no_file "app/controllers/admin/roles_controller.rb"
|
|
|
|
assert_no_file "test/functional/admin/roles_controller_test.rb"
|
|
|
|
|
|
|
|
# Views
|
|
|
|
assert_no_file "app/views/admin/roles"
|
|
|
|
assert_no_file "app/views/layouts/admin/roles.html.erb"
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
assert_no_file "app/helpers/admin/roles_helper.rb"
|
|
|
|
assert_no_file "test/unit/helpers/admin/roles_helper_test.rb"
|
|
|
|
|
2011-04-17 05:44:52 -04:00
|
|
|
# Assets
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css"
|
2011-06-27 20:54:03 -04:00
|
|
|
assert_no_file "app/assets/javascripts/admin/roles.js"
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_no_file "app/assets/stylesheets/admin/roles.css"
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
2010-08-02 04:12:35 -04:00
|
|
|
|
|
|
|
def test_scaffold_generator_on_revoke_does_not_mutilate_legacy_map_parameter
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
# Add a |map| parameter to the routes block manually
|
|
|
|
route_path = File.expand_path("config/routes.rb", destination_root)
|
|
|
|
content = File.read(route_path).gsub(/\.routes\.draw do/) do |match|
|
|
|
|
"#{match} |map|"
|
|
|
|
end
|
|
|
|
File.open(route_path, "wb") { |file| file.write(content) }
|
|
|
|
|
|
|
|
run_generator ["product_line"], :behavior => :revoke
|
|
|
|
|
|
|
|
assert_file "config/routes.rb", /\.routes\.draw do\s*\|map\|\s*$/
|
|
|
|
end
|
2010-09-02 06:45:22 -04:00
|
|
|
|
2011-04-17 05:44:52 -04:00
|
|
|
def test_scaffold_generator_no_assets
|
|
|
|
run_generator [ "posts", "--no-assets" ]
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css"
|
2011-06-27 20:54:03 -04:00
|
|
|
assert_no_file "app/assets/javascripts/posts.js"
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_no_file "app/assets/stylesheets/posts.css"
|
2011-04-17 05:44:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_scaffold_generator_no_stylesheets
|
|
|
|
run_generator [ "posts", "--no-stylesheets" ]
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_no_file "app/assets/stylesheets/scaffold.css"
|
2011-06-27 20:54:03 -04:00
|
|
|
assert_file "app/assets/javascripts/posts.js"
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_no_file "app/assets/stylesheets/posts.css"
|
2011-04-17 05:44:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_scaffold_generator_no_javascripts
|
|
|
|
run_generator [ "posts", "--no-javascripts" ]
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css"
|
2011-06-27 20:54:03 -04:00
|
|
|
assert_no_file "app/assets/javascripts/posts.js"
|
2011-04-17 05:44:52 -04:00
|
|
|
assert_file "app/assets/stylesheets/posts.css"
|
|
|
|
end
|
|
|
|
|
2010-09-02 06:45:22 -04:00
|
|
|
def test_scaffold_generator_outputs_error_message_on_missing_attribute_type
|
2011-06-17 14:10:53 -04:00
|
|
|
run_generator ["post", "title", "body:text", "author"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/create_posts.rb" do |m|
|
|
|
|
assert_method :change, m do |up|
|
|
|
|
assert_match(/t\.string :title/, up)
|
|
|
|
assert_match(/t\.text :body/, up)
|
|
|
|
assert_match(/t\.string :author/, up)
|
|
|
|
end
|
|
|
|
end
|
2010-09-02 06:45:22 -04:00
|
|
|
end
|
2009-07-01 16:06:05 -04:00
|
|
|
end
|