2009-07-01 14:12:29 -04:00
|
|
|
require 'generators/generators_test_helper'
|
2010-03-23 08:40:19 -04:00
|
|
|
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
|
2009-07-01 14:12:29 -04:00
|
|
|
|
2009-10-17 14:56:44 -04:00
|
|
|
module Unknown
|
|
|
|
module Generators
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2010-01-03 10:34:32 -05:00
|
|
|
arguments %w(User name:string age:integer)
|
2009-07-01 14:12:29 -04:00
|
|
|
|
|
|
|
def test_controller_skeleton_is_created
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
assert_file "app/controllers/users_controller.rb" do |content|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/class UsersController < ApplicationController/, content)
|
2009-07-01 14:12:29 -04:00
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :index, content do |m|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/@users = User\.all/, m)
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
|
|
|
|
2012-12-08 11:37:15 -05:00
|
|
|
assert_instance_method :show, content
|
2009-07-01 14:12:29 -04:00
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :new, content do |m|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/@user = User\.new/, m)
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
|
|
|
|
2012-12-08 11:37:15 -05:00
|
|
|
assert_instance_method :edit, content
|
2009-07-01 14:12:29 -04:00
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :create, content do |m|
|
2012-07-19 09:43:58 -04:00
|
|
|
assert_match(/@user = User\.new\(user_params\)/, m)
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/@user\.save/, m)
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :update, content do |m|
|
2013-01-02 16:55:29 -05:00
|
|
|
assert_match(/@user\.update\(user_params\)/, m)
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :destroy, content do |m|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/@user\.destroy/, m)
|
2013-02-09 12:10:45 -05:00
|
|
|
assert_match(/User was successfully destroyed/, m)
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
2012-07-19 09:43:58 -04:00
|
|
|
|
2012-12-08 11:37:15 -05:00
|
|
|
assert_instance_method :set_user, content do |m|
|
|
|
|
assert_match(/@user = User\.find\(params\[:id\]\)/, m)
|
|
|
|
end
|
|
|
|
|
2012-07-19 09:43:58 -04:00
|
|
|
assert_match(/def user_params/, content)
|
2012-09-23 11:54:45 -04:00
|
|
|
assert_match(/params\.require\(:user\)\.permit\(:name, :age\)/, content)
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-06 14:43:36 -05:00
|
|
|
def test_dont_use_require_or_permit_if_there_are_no_attributes
|
|
|
|
run_generator ["User"]
|
|
|
|
|
|
|
|
assert_file "app/controllers/users_controller.rb" do |content|
|
|
|
|
assert_match(/def user_params/, content)
|
|
|
|
assert_match(/params\[:user\]/, content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-06 14:48:11 -05:00
|
|
|
def test_controller_permit_references_attributes
|
|
|
|
run_generator ["LineItem", "product:references", "cart:belongs_to"]
|
|
|
|
|
|
|
|
assert_file "app/controllers/line_items_controller.rb" do |content|
|
|
|
|
assert_match(/def line_item_params/, content)
|
|
|
|
assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :cart_id\)/, content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-09 13:20:32 -05:00
|
|
|
def test_controller_permit_polymorphic_references_attributes
|
|
|
|
run_generator ["LineItem", "product:references{polymorphic}"]
|
|
|
|
|
|
|
|
assert_file "app/controllers/line_items_controller.rb" do |content|
|
|
|
|
assert_match(/def line_item_params/, content)
|
|
|
|
assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :product_type\)/, content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-01 14:12:29 -04:00
|
|
|
def test_helper_are_invoked_with_a_pluralized_name
|
|
|
|
run_generator
|
|
|
|
assert_file "app/helpers/users_helper.rb", /module UsersHelper/
|
2012-10-08 00:59:42 -04:00
|
|
|
assert_file "test/helpers/users_helper_test.rb", /class UsersHelperTest < ActionView::TestCase/
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_views_are_generated
|
|
|
|
run_generator
|
|
|
|
|
2012-11-04 16:22:05 -05:00
|
|
|
%w(index edit new show).each do |view|
|
|
|
|
assert_file "app/views/users/#{view}.html.erb"
|
|
|
|
end
|
2010-04-09 13:04:20 -04:00
|
|
|
assert_no_file "app/views/layouts/users.html.erb"
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_functional_tests
|
2012-12-09 13:20:32 -05:00
|
|
|
run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}"]
|
2009-07-01 14:12:29 -04:00
|
|
|
|
2012-10-08 00:59:42 -04:00
|
|
|
assert_file "test/controllers/users_controller_test.rb" do |content|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
|
|
|
|
assert_match(/test "should get index"/, content)
|
2012-12-09 13:20:32 -05:00
|
|
|
assert_match(/post :create, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content)
|
2013-01-03 06:21:18 -05:00
|
|
|
assert_match(/patch :update, id: @user, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content)
|
2012-03-12 19:10:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_functional_tests_without_attributes
|
|
|
|
run_generator ["User"]
|
|
|
|
|
2012-10-08 00:59:42 -04:00
|
|
|
assert_file "test/controllers/users_controller_test.rb" do |content|
|
2012-03-12 19:10:40 -04:00
|
|
|
assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
|
|
|
|
assert_match(/test "should get index"/, content)
|
2012-03-13 13:19:03 -04:00
|
|
|
assert_match(/post :create, user: \{ \}/, content)
|
2013-01-03 06:21:18 -05:00
|
|
|
assert_match(/patch :update, id: @user, user: \{ \}/, content)
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_skip_helper_if_required
|
|
|
|
run_generator ["User", "name:string", "age:integer", "--no-helper"]
|
|
|
|
assert_no_file "app/helpers/users_helper.rb"
|
2012-10-08 00:59:42 -04:00
|
|
|
assert_no_file "test/helpers/users_helper_test.rb"
|
2009-07-01 14:12:29 -04:00
|
|
|
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
|
|
|
|
|
2009-10-17 14:56:44 -04:00
|
|
|
def test_default_orm_is_used
|
|
|
|
run_generator ["User", "--orm=unknown"]
|
|
|
|
|
|
|
|
assert_file "app/controllers/users_controller.rb" do |content|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/class UsersController < ApplicationController/, content)
|
2009-10-17 14:56:44 -04:00
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :index, content do |m|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/@users = User\.all/, m)
|
2009-10-17 14:56:44 -04:00
|
|
|
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|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/class UsersController < ApplicationController/, content)
|
2009-10-17 14:56:44 -04:00
|
|
|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :index, content do |m|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/@users = User\.find\(:all\)/, m)
|
|
|
|
assert_no_match(/@users = User\.all/, m)
|
2009-10-17 14:56:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Unknown::Generators.send :remove_const, :ActiveModel
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|
2011-04-10 00:27:26 -04:00
|
|
|
|
|
|
|
def test_new_hash_style
|
|
|
|
run_generator
|
|
|
|
assert_file "app/controllers/users_controller.rb" do |content|
|
2013-01-10 22:26:39 -05:00
|
|
|
assert_match(/render action: 'new'/, content)
|
2011-04-10 00:38:59 -04:00
|
|
|
end
|
|
|
|
end
|
2009-07-01 14:12:29 -04:00
|
|
|
end
|