-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathscaffold_controller_generator_test.rb
112 lines (94 loc) · 4.43 KB
/
scaffold_controller_generator_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'test_helper'
require 'rails/generators/test_case'
require 'generators/rails/scaffold_controller_generator'
class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::ScaffoldControllerGenerator
arguments %w(Post title body:text images:attachments)
destination File.expand_path('../tmp', __FILE__)
setup :prepare_destination
test 'controller content' do
run_generator
assert_file 'app/controllers/posts_controller.rb' do |content|
assert_instance_method :index, content do |m|
assert_match %r{@posts = Post\.all}, m
end
assert_instance_method :show, content do |m|
assert m.blank?
end
assert_instance_method :new, content do |m|
assert_match %r{@post = Post\.new}, m
end
assert_instance_method :edit, content do |m|
assert m.blank?
end
assert_instance_method :create, content do |m|
assert_match %r{@post = Post\.new\(post_params\)}, m
assert_match %r{@post\.save}, m
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m
assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m
assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m
end
assert_instance_method :update, content do |m|
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\.", status: :see_other \}}, m
assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m
assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m
end
assert_instance_method :destroy, content do |m|
assert_match %r{@post\.destroy}, m
assert_match %r{format\.html \{ redirect_to posts_path, notice: "Post was successfully destroyed\.", status: :see_other \}}, m
assert_match %r{format\.json \{ head :no_content \}}, m
end
assert_match %r{def set_post}, content
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(:id\)}, content
else
assert_match %r{params\[:id\]}, content
end
assert_match %r{def post_params}, content
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(post: \[ :title, :body, images: \[\] \]\)}, content
elsif Rails::VERSION::MAJOR >= 6
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, images: \[\]\)}, content
else
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, :images\)}, content
end
end
end
if Rails::VERSION::MAJOR >= 6
test 'controller with namespace' do
run_generator %w(Admin::Post --model-name=Post)
assert_file 'app/controllers/admin/posts_controller.rb' do |content|
assert_instance_method :create, content do |m|
assert_match %r{format\.html \{ redirect_to \[:admin, @post\], notice: "Post was successfully created\." \}}, m
end
assert_instance_method :update, content do |m|
assert_match %r{format\.html \{ redirect_to \[:admin, @post\], notice: "Post was successfully updated\.", status: :see_other \}}, m
end
assert_instance_method :destroy, content do |m|
assert_match %r{format\.html \{ redirect_to admin_posts_path, notice: "Post was successfully destroyed\.", status: :see_other \}}, m
end
end
end
end
test "don't use require and permit if there are no attributes" do
run_generator %w(Post)
assert_file 'app/controllers/posts_controller.rb' do |content|
assert_match %r{def post_params}, content
assert_match %r{params\.fetch\(:post, \{\}\)}, content
end
end
if Rails::VERSION::MAJOR >= 6
test 'handles virtual attributes' do
run_generator %w(Message content:rich_text video:attachment photos:attachments)
assert_file 'app/controllers/messages_controller.rb' do |content|
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(message: \[ :content, :video, photos: \[\] \]\)}, content
else
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
end
end
end
end
end