File: controller_generator_spec.rb

package info (click to toggle)
ruby-rspec-rails 7.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,796 kB
  • sloc: ruby: 11,068; sh: 198; makefile: 6
file content (232 lines) | stat: -rw-r--r-- 6,923 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
# Generators are not automatically loaded by Rails
require 'generators/rspec/controller/controller_generator'
require 'support/generators'

RSpec.describe Rspec::Generators::ControllerGenerator, type: :generator do
  setup_default_destination

  describe 'request specs' do
    subject(:filename) { file('spec/requests/posts_spec.rb') }

    describe 'generated by default' do
      before do
        run_generator %w[posts]
      end

      it 'includes the standard boilerplate' do
        expect(filename).to contain(/require 'rails_helper'/)
                              .and(contain(/^RSpec.describe "Posts", #{type_metatag(:request)}/))
                              .and(contain('pending'))
      end
    end

    describe 'skipped with a flag' do
      before do
        run_generator %w[posts --no-request_specs]
      end

      it 'skips the file' do
        expect(File.exist?(filename)).to be false
      end
    end

    describe 'with actions' do
      before do
        run_generator %w[posts index custom_action]
      end

      it 'includes the standard boilerplate' do
        expect(filename).to contain('get "/posts/index"')
                             .and(contain('get "/posts/custom_action"'))
      end
    end

    describe 'with namespace and actions' do
      subject(:filename) { file('spec/requests/admin/external/users_spec.rb') }

      before do
        run_generator %w[admin::external::users index custom_action]
      end

      it 'includes the standard boilerplate' do
        expect(filename).to contain(/^RSpec.describe "Admin::External::Users", #{type_metatag(:request)}/)
                             .and(contain('get "/admin/external/users/index"'))
                             .and(contain('get "/admin/external/users/custom_action"'))
      end
    end
  end

  describe 'view specs' do
    describe 'are not generated' do
      describe 'with no-view-spec flag' do
        before do
          run_generator %w[posts index show --no-view-specs]
        end

        describe 'index.html.erb' do
          subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') }

          it 'skips the file' do
            expect(File.exist?(filename)).to be false
          end
        end
      end

      describe 'with no actions' do
        before do
          run_generator %w[posts]
        end

        describe 'index.html.erb' do
          subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') }

          it 'skips the file' do
            expect(File.exist?(filename)).to be false
          end
        end
      end

      describe 'with --no-template-engine' do
        before do
          run_generator %w[posts index --no-template-engine]
        end

        describe 'index.html.erb' do
          subject(:filename) { file('spec/views/posts/index.html._spec.rb') }

          it 'skips the file' do
            expect(File.exist?(filename)).to be false
          end
        end
      end
    end

    describe 'are generated' do
      describe 'with default template engine' do
        before do
          run_generator %w[posts index show]
        end

        describe 'index.html.erb' do
          subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') }

          it 'includes the standard boilerplate' do
            expect(filename).to contain(/require 'rails_helper'/)
                                 .and(contain(/^RSpec.describe "posts\/index.html.erb", #{type_metatag(:view)}/))
          end
        end

        describe 'show.html.erb' do
          subject(:filename) { file('spec/views/posts/show.html.erb_spec.rb') }

          it 'includes the standard boilerplate' do
            expect(filename).to contain(/require 'rails_helper'/)
                                 .and(contain(/^RSpec.describe "posts\/show.html.erb", #{type_metatag(:view)}/))
          end
        end
      end

      describe 'with haml' do
        before do
          run_generator %w[posts index --template_engine haml]
        end

        describe 'index.html.haml' do
          subject(:filename) { file('spec/views/posts/index.html.haml_spec.rb') }

          it 'includes the standard boilerplate' do
            expect(filename).to contain(/require 'rails_helper'/)
                                 .and(contain(/^RSpec.describe "posts\/index.html.haml", #{type_metatag(:view)}/))
          end
        end
      end
    end

    describe 'are removed' do
      subject(:output) { run_generator %w[posts], behavior: :revoke }

      it 'will remove the file' do
        expect(output).to match('remove  spec/views/posts')
      end
    end
  end

  describe 'routing spec' do
    subject(:filename) { file('spec/routing/posts_routing_spec.rb') }

    describe 'with no flag' do
      before do
        run_generator %w[posts seek and destroy]
      end

      it 'skips the file' do
        expect(File.exist?(filename)).to be false
      end
    end

    describe 'with --routing-specs  flag' do
      describe 'without action parameter' do
        before do
          run_generator %w[posts --routing-specs]
        end

        it 'skips the file' do
          expect(File.exist?(filename)).to be false
        end
      end

      describe 'with action parameter' do
        before { run_generator %w[posts seek --routing-specs] }

        it 'includes the standard boilerplate' do
          expect(filename).to contain(/require 'rails_helper'/)
                                .and(contain(/^RSpec.describe 'PostsController', #{type_metatag(:routing)}/))
                                .and(contain(/describe 'routing'/))
                                .and(contain(/it 'routes to #seek'/))
                                .and(contain(/expect\(get: "\/posts\/seek"\).to route_to\("posts#seek"\)/))
        end
      end
    end

    describe 'with --no-routing-specs flag' do
      before do
        run_generator %w[posts seek and destroy --no-routing_specs]
      end

      it 'skips the file' do
        expect(File.exist?(filename)).to be false
      end
    end
  end

  describe 'controller specs' do
    subject(:filename) { file('spec/controllers/posts_controller_spec.rb') }

    it 'are not generated' do
      expect(File.exist?(filename)).to be false
    end

    describe 'with --controller-specs flag' do
      before do
        run_generator %w[posts --controller-specs]
      end

      describe 'the spec' do
        it 'includes the standard boilerplate' do
          expect(filename).to contain(/require 'rails_helper'/)
                               .and(contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/))
        end
      end
    end

    describe 'with --no-controller_specs flag' do
      before do
        run_generator %w[posts --no-controller-specs]
      end

      it 'are skipped' do
        expect(File.exist?(filename)).to be false
      end
    end
  end
end