aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/scaffold_generator_test.rb
blob: ea469cb3c8e4efa6ce77ac6c5e5c24d7bc72aaf7 (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
require 'generators/generators_test_helper'
require 'rails/generators/rails/scaffold/scaffold_generator'

class ScaffoldGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
  arguments %w(product_line title:string price:integer)

  setup :copy_routes

  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"
    assert_migration "db/migrate/create_product_lines.rb"

    # Route
    assert_file "config/routes.rb" do |route|
      assert_match /resources :product_lines$/, route
    end

    # Controller
    assert_file "app/controllers/product_lines_controller.rb" do |content|
      assert_match /class ProductLinesController < ApplicationController/, content

      assert_instance_method :index, content do |m|
        assert_match /@product_lines = ProductLine\.all/, m
      end

      assert_instance_method :show, content do |m|
        assert_match /@product_line = ProductLine\.find\(params\[:id\]\)/, m
      end

      assert_instance_method :new, content do |m|
        assert_match /@product_line = ProductLine\.new/, m
      end

      assert_instance_method :edit, content do |m|
        assert_match /@product_line = ProductLine\.find\(params\[:id\]\)/, m
      end

      assert_instance_method :create, content do |m|
        assert_match /@product_line = ProductLine\.new\(params\[:product_line\]\)/, m
        assert_match /@product_line\.save/, m
        assert_match /@product_line\.errors/, m
      end

      assert_instance_method :update, content do |m|
        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
      end

      assert_instance_method :destroy, content do |m|
        assert_match /@product_line = ProductLine\.find\(params\[:id\]\)/, m
        assert_match /@product_line\.destroy/, m
      end
    end

    assert_file "test/functional/product_lines_controller_test.rb",
                /class ProductLinesControllerTest < ActionController::TestCase/

    # Views
    %w(
      index
      edit
      new
      show
      _form
    ).each { |view| assert_file "app/views/product_lines/#{view}.html.erb" }
    assert_no_file "app/views/layouts/product_lines.html.erb"

    # Helpers
    assert_file "app/helpers/product_lines_helper.rb"
    assert_file "test/unit/helpers/product_lines_helper_test.rb"

    # Stylesheets
    assert_file "public/stylesheets/scaffold.css"
  end

  def test_scaffold_on_revoke
    run_generator
    run_generator ["product_line"], :behavior => :revoke

    # 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|
      assert_no_match /resources :product_lines$/, route
    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"
    assert_no_file "app/views/layouts/product_lines.html.erb"

    # Helpers
    assert_no_file "app/helpers/product_lines_helper.rb"
    assert_no_file "test/unit/helpers/product_lines_helper_test.rb"

    # Stylesheets (should not be removed)
    assert_file "public/stylesheets/scaffold.css"
  end

  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|
      assert_match /namespace :admin do resources :roles end$/, route
    end

    # Controller
    assert_file "app/controllers/admin/roles_controller.rb" do |content|
      assert_match /class Admin::RolesController < ApplicationController/, content

      assert_instance_method :index, content do |m|
        assert_match /@admin_roles = Admin::Role\.all/, m
      end

      assert_instance_method :show, content do |m|
        assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m
      end

      assert_instance_method :new, content do |m|
        assert_match /@admin_role = Admin::Role\.new/, m
      end

      assert_instance_method :edit, content do |m|
        assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m
      end

      assert_instance_method :create, content do |m|
        assert_match /@admin_role = Admin::Role\.new\(params\[:admin_role\]\)/, m
        assert_match /@admin_role\.save/, m
        assert_match /@admin_role\.errors/, m
      end

      assert_instance_method :update, content do |m|
        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
      end

      assert_instance_method :destroy, content do |m|
        assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m
        assert_match /@admin_role\.destroy/, m
      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"

    # Stylesheets
    assert_file "public/stylesheets/scaffold.css"
  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|
      assert_no_match /namespace :admin do resources :roles end$/, route
    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"

    # Stylesheets (should not be removed)
    assert_file "public/stylesheets/scaffold.css"
  end
end