rspec-rails 2.14.2 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +4 -2
  5. data/Capybara.md +6 -57
  6. data/Changelog.md +859 -37
  7. data/{License.txt → LICENSE.md} +5 -2
  8. data/README.md +304 -372
  9. data/lib/generators/rspec/channel/channel_generator.rb +12 -0
  10. data/lib/generators/rspec/{observer/templates/observer_spec.rb → channel/templates/channel_spec.rb.erb} +2 -2
  11. data/lib/generators/rspec/controller/controller_generator.rb +25 -7
  12. data/lib/generators/rspec/controller/templates/controller_spec.rb +5 -5
  13. data/lib/generators/rspec/controller/templates/request_spec.rb +19 -0
  14. data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
  15. data/lib/generators/rspec/controller/templates/view_spec.rb +2 -2
  16. data/lib/generators/rspec/feature/feature_generator.rb +29 -0
  17. data/lib/generators/rspec/feature/templates/feature_singular_spec.rb +5 -0
  18. data/lib/generators/rspec/feature/templates/feature_spec.rb +5 -0
  19. data/lib/generators/rspec/generator/generator_generator.rb +24 -0
  20. data/lib/generators/rspec/generator/templates/generator_spec.rb +6 -0
  21. data/lib/generators/rspec/helper/helper_generator.rb +3 -2
  22. data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
  23. data/lib/generators/rspec/install/install_generator.rb +61 -5
  24. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +84 -0
  25. data/lib/generators/rspec/integration/integration_generator.rb +15 -13
  26. data/lib/generators/rspec/job/job_generator.rb +13 -0
  27. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  28. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  29. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  30. data/lib/generators/rspec/mailer/mailer_generator.rb +11 -3
  31. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +7 -7
  32. data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
  33. data/lib/generators/rspec/model/model_generator.rb +21 -6
  34. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  35. data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
  36. data/lib/generators/rspec/request/request_generator.rb +17 -0
  37. data/lib/generators/rspec/request/templates/request_spec.rb +10 -0
  38. data/lib/generators/rspec/scaffold/scaffold_generator.rb +91 -147
  39. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +129 -0
  40. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  41. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +74 -82
  42. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +13 -17
  43. data/lib/generators/rspec/scaffold/templates/index_spec.rb +6 -12
  44. data/lib/generators/rspec/scaffold/templates/new_spec.rb +7 -15
  45. data/lib/generators/rspec/scaffold/templates/request_spec.rb +153 -0
  46. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +19 -12
  47. data/lib/generators/rspec/scaffold/templates/show_spec.rb +5 -12
  48. data/lib/generators/rspec/system/system_generator.rb +24 -0
  49. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  50. data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
  51. data/lib/generators/rspec/view/view_generator.rb +5 -4
  52. data/lib/generators/rspec.rb +35 -10
  53. data/lib/rspec/rails/active_record.rb +25 -0
  54. data/lib/rspec/rails/adapters.rb +60 -47
  55. data/lib/rspec/rails/configuration.rb +220 -0
  56. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  57. data/lib/rspec/rails/example/controller_example_group.rb +189 -138
  58. data/lib/rspec/rails/example/feature_example_group.rb +43 -20
  59. data/lib/rspec/rails/example/helper_example_group.rb +28 -26
  60. data/lib/rspec/rails/example/job_example_group.rb +23 -0
  61. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  62. data/lib/rspec/rails/example/mailer_example_group.rb +30 -14
  63. data/lib/rspec/rails/example/model_example_group.rb +8 -7
  64. data/lib/rspec/rails/example/rails_example_group.rb +12 -2
  65. data/lib/rspec/rails/example/request_example_group.rb +21 -17
  66. data/lib/rspec/rails/example/routing_example_group.rb +49 -40
  67. data/lib/rspec/rails/example/system_example_group.rb +172 -0
  68. data/lib/rspec/rails/example/view_example_group.rb +179 -134
  69. data/lib/rspec/rails/example.rb +4 -33
  70. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -11
  71. data/lib/rspec/rails/extensions.rb +0 -1
  72. data/lib/rspec/rails/feature_check.rb +51 -0
  73. data/lib/rspec/rails/file_fixture_support.rb +18 -0
  74. data/lib/rspec/rails/fixture_file_upload_support.rb +45 -0
  75. data/lib/rspec/rails/fixture_support.rb +74 -24
  76. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +173 -0
  77. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  78. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  79. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  80. data/lib/rspec/rails/matchers/active_job.rb +465 -0
  81. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  82. data/lib/rspec/rails/matchers/be_a_new.rb +69 -62
  83. data/lib/rspec/rails/matchers/be_new_record.rb +24 -21
  84. data/lib/rspec/rails/matchers/be_valid.rb +42 -33
  85. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +227 -0
  86. data/lib/rspec/rails/matchers/have_http_status.rb +385 -0
  87. data/lib/rspec/rails/matchers/have_rendered.rb +55 -31
  88. data/lib/rspec/rails/matchers/redirect_to.rb +30 -29
  89. data/lib/rspec/rails/matchers/relation_match_array.rb +2 -2
  90. data/lib/rspec/rails/matchers/routing_matchers.rb +107 -93
  91. data/lib/rspec/rails/matchers/send_email.rb +122 -0
  92. data/lib/rspec/rails/matchers.rb +23 -14
  93. data/lib/rspec/rails/tasks/rspec.rake +8 -18
  94. data/lib/rspec/rails/vendor/capybara.rb +10 -11
  95. data/lib/rspec/rails/version.rb +3 -1
  96. data/lib/rspec/rails/view_assigns.rb +3 -21
  97. data/lib/rspec/rails/view_path_builder.rb +29 -0
  98. data/lib/rspec/rails/view_rendering.rb +102 -63
  99. data/lib/rspec/rails/view_spec_methods.rb +56 -0
  100. data/lib/rspec/rails.rb +10 -10
  101. data/lib/rspec-rails.rb +69 -3
  102. data.tar.gz.sig +0 -0
  103. metadata +123 -95
  104. metadata.gz.sig +0 -0
  105. data/lib/autotest/rails_rspec2.rb +0 -85
  106. data/lib/generators/rspec/install/templates/.rspec +0 -1
  107. data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +0 -49
  108. data/lib/generators/rspec/integration/templates/request_spec.rb +0 -15
  109. data/lib/generators/rspec/observer/observer_generator.rb +0 -12
  110. data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
  111. data/lib/rspec/rails/matchers/have_extension.rb +0 -36
  112. data/lib/rspec/rails/mocks.rb +0 -274
  113. data/lib/rspec/rails/module_inclusion.rb +0 -19
  114. data/lib/rspec/rails/vendor/webrat.rb +0 -33
@@ -0,0 +1,17 @@
1
+ require 'generators/rspec'
2
+
3
+ module Rspec
4
+ module Generators
5
+ # @private
6
+ class RequestGenerator < Base
7
+ class_option :request_specs, type: :boolean, default: true, desc: 'Generate request specs'
8
+
9
+ def generate_request_spec
10
+ return unless options[:request_specs]
11
+
12
+ template 'request_spec.rb',
13
+ target_path('requests', "#{name.underscore.pluralize}_spec.rb")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do
4
+ describe "GET /<%= name.underscore.pluralize %>" do
5
+ it "works! (now write some real specs)" do
6
+ get <%= index_helper %>_path
7
+ expect(response).to have_http_status(200)
8
+ end
9
+ end
10
+ end
@@ -3,30 +3,50 @@ require 'rails/generators/resource_helpers'
3
3
 
4
4
  module Rspec
5
5
  module Generators
6
+ # @private
6
7
  class ScaffoldGenerator < Base
7
8
  include ::Rails::Generators::ResourceHelpers
8
- source_paths << File.expand_path("../../helper/templates", __FILE__)
9
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
10
-
11
- class_option :orm, :desc => "ORM used to generate the controller"
12
- class_option :template_engine, :desc => "Template engine to generate view files"
13
- class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
14
-
15
- class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
16
- class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
17
- class_option :webrat, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers"
18
- class_option :webrat_matchers, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers (deprecated - use --webrat)"
19
- class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
20
- class_option :routing_specs, :type => :boolean, :default => true, :desc => "Generate routing specs"
9
+ source_paths << File.expand_path('../helper/templates', __dir__)
10
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
11
+
12
+ class_option :orm, desc: "ORM used to generate the controller"
13
+ class_option :template_engine, desc: "Template engine to generate view files"
14
+ class_option :singleton, type: :boolean, desc: "Supply to create a singleton controller"
15
+ class_option :api, type: :boolean, desc: "Skip specs unnecessary for API-only apps"
16
+
17
+ class_option :controller_specs, type: :boolean, default: false, desc: "Generate controller specs"
18
+ class_option :request_specs, type: :boolean, default: true, desc: "Generate request specs"
19
+ class_option :view_specs, type: :boolean, default: true, desc: "Generate view specs"
20
+ class_option :helper_specs, type: :boolean, default: true, desc: "Generate helper specs"
21
+ class_option :routing_specs, type: :boolean, default: true, desc: "Generate routing specs"
22
+
23
+ def initialize(*args, &blk)
24
+ @generator_args = args.first
25
+ super(*args, &blk)
26
+ end
21
27
 
22
28
  def generate_controller_spec
23
29
  return unless options[:controller_specs]
24
30
 
25
- template 'controller_spec.rb',
26
- File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
31
+ if options[:api]
32
+ template 'api_controller_spec.rb', template_file(folder: 'controllers', suffix: '_controller')
33
+ else
34
+ template 'controller_spec.rb', template_file(folder: 'controllers', suffix: '_controller')
35
+ end
36
+ end
37
+
38
+ def generate_request_spec
39
+ return unless options[:request_specs]
40
+
41
+ if options[:api]
42
+ template 'api_request_spec.rb', template_file(folder: 'requests')
43
+ else
44
+ template 'request_spec.rb', template_file(folder: 'requests')
45
+ end
27
46
  end
28
47
 
29
48
  def generate_view_specs
49
+ return if options[:api]
30
50
  return unless options[:view_specs] && options[:template_engine]
31
51
 
32
52
  copy_view :edit
@@ -38,155 +58,79 @@ module Rspec
38
58
  def generate_routing_spec
39
59
  return unless options[:routing_specs]
40
60
 
41
- template 'routing_spec.rb',
42
- File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")
61
+ template_file = target_path(
62
+ 'routing',
63
+ controller_class_path,
64
+ "#{controller_file_name}_routing_spec.rb"
65
+ )
66
+ template 'routing_spec.rb', template_file
43
67
  end
44
68
 
45
- hook_for :integration_tool, :as => :integration
69
+ protected
46
70
 
47
- protected
48
-
49
- # @deprecated Use `--webrat` instead.
50
- def webrat?
51
- RSpec.deprecate("--webrat-matchers", :replacement => "--webrat") if options[:webrat_matchers]
52
- options[:webrat] || options[:webrat_matchers]
53
- end
71
+ attr_reader :generator_args
54
72
 
55
- def copy_view(view)
56
- template "#{view}_spec.rb",
57
- File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
58
- end
59
-
60
- def example_valid_attributes
61
- # Only take the first attribute so this hash does not become unweildy and large in the
62
- # generated controller spec. It is the responsibility of the user to keep the the valid
63
- # attributes method up-to-date as they add validations.
64
- @example_valid_attributes ||=
65
- if attributes.any?
66
- { attributes.first.name => attributes.first.default.to_s }
67
- else
68
- { }
69
- end
70
- end
71
-
72
- def example_invalid_attributes
73
- @example_invalid_attributes ||=
74
- if attributes.any?
75
- { attributes.first.name => "invalid value" }
76
- else
77
- { }
78
- end
79
- end
80
-
81
- def example_params_for_update
82
- @example_params_for_update ||=
83
- if example_valid_attributes.any?
84
- example_valid_attributes
85
- else
86
- { "these" => "params" }
87
- end
88
- end
73
+ def copy_view(view)
74
+ template "#{view}_spec.rb",
75
+ target_path("views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
76
+ end
89
77
 
90
- def formatted_hash(hash)
91
- formatted = hash.inspect
92
- formatted.gsub!("{", "{ ")
93
- formatted.gsub!("}", " }")
94
- formatted.gsub!("=>", " => ")
95
- formatted
96
- end
78
+ # support for namespaced-resources
79
+ def ns_file_name
80
+ return file_name if ns_parts.empty?
97
81
 
98
- # support for namespaced-resources
99
- def ns_file_name
100
- ns_parts.empty? ? file_name : "#{ns_parts[0].underscore}_#{ns_parts[1].singularize.underscore}"
101
- end
82
+ "#{ns_prefix.map(&:underscore).join('/')}_#{ns_suffix.singularize.underscore}"
83
+ end
102
84
 
103
- # support for namespaced-resources
104
- def ns_table_name
105
- ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}"
106
- end
85
+ # support for namespaced-resources
86
+ def ns_table_name
87
+ return table_name if ns_parts.empty?
107
88
 
108
- def ns_parts
109
- @ns_parts ||= begin
110
- matches = ARGV[0].to_s.match(/\A(\w+)(?:\/|::)(\w+)/)
111
- matches ? [matches[1], matches[2]] : []
112
- end
113
- end
89
+ "#{ns_prefix.map(&:underscore).join('/')}/#{ns_suffix.tableize}"
90
+ end
114
91
 
115
- # Returns the name of the mock. For example, if the file name is user,
116
- # it returns mock_user.
117
- #
118
- # If a hash is given, it uses the hash key as the ORM method and the
119
- # value as response. So, for ActiveRecord and file name "User":
120
- #
121
- # mock_file_name(:save => true)
122
- # #=> mock_user(:save => true)
123
- #
124
- # If another ORM is being used and another method instead of save is
125
- # called, it will be the one used.
126
- #
127
- def mock_file_name(hash=nil)
128
- if hash
129
- method, and_return = hash.to_a.first
130
- method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '')
131
- "mock_#{ns_file_name}(:#{method} => #{and_return})"
132
- else
133
- "mock_#{ns_file_name}"
134
- end
135
- end
92
+ def ns_parts
93
+ @ns_parts ||= begin
94
+ parts = generator_args[0].split(/\/|::/)
95
+ parts.size > 1 ? parts : []
96
+ end
97
+ end
136
98
 
137
- # Receives the ORM chain and convert to expects. For ActiveRecord:
138
- #
139
- # should! orm_class.find(User, "37")
140
- # #=> User.should_receive(:find).with(37)
141
- #
142
- # For Datamapper:
143
- #
144
- # should! orm_class.find(User, "37")
145
- # #=> User.should_receive(:get).with(37)
146
- #
147
- def should_receive(chain)
148
- stub_or_should_chain(:should_receive, chain)
149
- end
99
+ def ns_prefix
100
+ @ns_prefix ||= ns_parts[0..-2]
101
+ end
150
102
 
151
- # Receives the ORM chain and convert to stub. For ActiveRecord:
152
- #
153
- # stub orm_class.find(User, "37")
154
- # #=> User.stub(:find).with(37)
155
- #
156
- # For Datamapper:
157
- #
158
- # stub orm_class.find(User, "37")
159
- # #=> User.stub(:get).with(37)
160
- #
161
- def stub(chain)
162
- stub_or_should_chain(:stub, chain)
163
- end
103
+ def ns_suffix
104
+ @ns_suffix ||= ns_parts[-1]
105
+ end
164
106
 
165
- def stub_or_should_chain(mode, chain)
166
- receiver, method = chain.split(".")
167
- method.gsub!(/\((.*?)\)/, '')
107
+ def value_for(attribute)
108
+ raw_value_for(attribute).inspect
109
+ end
168
110
 
169
- response = "#{receiver}.#{mode}(:#{method})"
170
- response << ".with(#{$1})" unless $1.blank?
171
- response
111
+ def raw_value_for(attribute)
112
+ case attribute.type
113
+ when :string
114
+ attribute.name.titleize
115
+ when :integer, :float
116
+ @attribute_id_map ||= {}
117
+ @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next + attribute.default
118
+ else
119
+ attribute.default
172
120
  end
121
+ end
173
122
 
174
- def value_for(attribute)
175
- case attribute.type
176
- when :string
177
- "#{attribute.name.titleize}".inspect
178
- when :integer
179
- @attribute_id_map ||= {}
180
- @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next.to_s
181
- else
182
- attribute.default.inspect
183
- end
184
- end
123
+ def template_file(folder:, suffix: '')
124
+ target_path(folder, controller_class_path, "#{controller_file_name}#{suffix}_spec.rb")
125
+ end
185
126
 
186
- def banner
187
- self.class.banner
188
- end
127
+ def banner
128
+ self.class.banner
129
+ end
189
130
 
131
+ def show_helper(resource_name = file_name)
132
+ "#{singular_route_name}_url(#{resource_name})"
133
+ end
190
134
  end
191
135
  end
192
136
  end
@@ -0,0 +1,129 @@
1
+ require 'rails_helper'
2
+
3
+ # This spec was generated by rspec-rails when you ran the scaffold generator.
4
+ # It demonstrates how one might use RSpec to specify the controller code that
5
+ # was generated by Rails when you ran the scaffold generator.
6
+ #
7
+ # It assumes that the implementation code is generated by the rails scaffold
8
+ # generator. If you are using any extension libraries to generate different
9
+ # controller code, this generated spec may or may not pass.
10
+ #
11
+ # It only uses APIs available in rails and/or rspec-rails. There are a number
12
+ # of tools you can use to make these specs even more expressive, but we're
13
+ # sticking to rails and rspec-rails APIs to keep things simple and stable.
14
+ #
15
+ # Compared to earlier versions of this generator, there is very limited use of
16
+ # stubs and message expectations in this spec. Stubs are only used when there
17
+ # is no simpler way to get a handle on the object needed for the example.
18
+ # Message expectations are only used when there is no simpler way to specify
19
+ # that an instance is receiving a specific message.
20
+ #
21
+ # Also compared to earlier versions of this generator, there are no longer any
22
+ # expectations of assigns and templates rendered. These features have been
23
+ # removed from Rails core in Rails 5, but can be added back in via the
24
+ # `rails-controller-testing` gem.
25
+
26
+ <% module_namespacing do -%>
27
+ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:controller) %> do
28
+
29
+ # This should return the minimal set of attributes required to create a valid
30
+ # <%= class_name %>. As you add validations to <%= class_name %>, be sure to
31
+ # adjust the attributes here as well.
32
+ let(:valid_attributes) {
33
+ skip("Add a hash of attributes valid for your model")
34
+ }
35
+
36
+ let(:invalid_attributes) {
37
+ skip("Add a hash of attributes invalid for your model")
38
+ }
39
+
40
+ # This should return the minimal set of values that should be in the session
41
+ # in order to pass any filters (e.g. authentication) defined in
42
+ # <%= controller_class_name %>Controller. Be sure to keep this updated too.
43
+ let(:valid_session) { {} }
44
+
45
+ <% unless options[:singleton] -%>
46
+ describe "GET #index" do
47
+ it "returns a success response" do
48
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
49
+ get :index, params: {}, session: valid_session
50
+ expect(response).to be_successful
51
+ end
52
+ end
53
+
54
+ <% end -%>
55
+ describe "GET #show" do
56
+ it "returns a success response" do
57
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
58
+ get :show, params: {id: <%= file_name %>.to_param}, session: valid_session
59
+ expect(response).to be_successful
60
+ end
61
+ end
62
+
63
+ describe "POST #create" do
64
+ context "with valid params" do
65
+ it "creates a new <%= class_name %>" do
66
+ expect {
67
+ post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
68
+ }.to change(<%= class_name %>, :count).by(1)
69
+ end
70
+
71
+ it "renders a JSON response with the new <%= singular_table_name %>" do
72
+ post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
73
+ expect(response).to have_http_status(:created)
74
+ expect(response.content_type).to eq('application/json')
75
+ expect(response.location).to eq(<%= singular_table_name %>_url(<%= class_name %>.last))
76
+ end
77
+ end
78
+
79
+ context "with invalid params" do
80
+ it "renders a JSON response with errors for the new <%= singular_table_name %>" do
81
+ post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
82
+ expect(response).to have_http_status(:unprocessable_entity)
83
+ expect(response.content_type).to eq('application/json')
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "PUT #update" do
89
+ context "with valid params" do
90
+ let(:new_attributes) {
91
+ skip("Add a hash of attributes valid for your model")
92
+ }
93
+
94
+ it "updates the requested <%= singular_table_name %>" do
95
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
96
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
97
+ <%= file_name %>.reload
98
+ skip("Add assertions for updated state")
99
+ end
100
+
101
+ it "renders a JSON response with the <%= singular_table_name %>" do
102
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
103
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
104
+ expect(response).to have_http_status(:ok)
105
+ expect(response.content_type).to eq('application/json')
106
+ end
107
+ end
108
+
109
+ context "with invalid params" do
110
+ it "renders a JSON response with errors for the <%= singular_table_name %>" do
111
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
112
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
113
+ expect(response).to have_http_status(:unprocessable_entity)
114
+ expect(response.content_type).to eq('application/json')
115
+ end
116
+ end
117
+ end
118
+
119
+ describe "DELETE #destroy" do
120
+ it "destroys the requested <%= singular_table_name %>" do
121
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
122
+ expect {
123
+ delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
124
+ }.to change(<%= class_name %>, :count).by(-1)
125
+ end
126
+ end
127
+
128
+ end
129
+ <% end -%>
@@ -0,0 +1,131 @@
1
+ require 'rails_helper'
2
+
3
+ # This spec was generated by rspec-rails when you ran the scaffold generator.
4
+ # It demonstrates how one might use RSpec to test the controller code that
5
+ # was generated by Rails when you ran the scaffold generator.
6
+ #
7
+ # It assumes that the implementation code is generated by the rails scaffold
8
+ # generator. If you are using any extension libraries to generate different
9
+ # controller code, this generated spec may or may not pass.
10
+ #
11
+ # It only uses APIs available in rails and/or rspec-rails. There are a number
12
+ # of tools you can use to make these specs even more expressive, but we're
13
+ # sticking to rails and rspec-rails APIs to keep things simple and stable.
14
+
15
+ <% module_namespacing do -%>
16
+ RSpec.describe "/<%= name.underscore.pluralize %>", <%= type_metatag(:request) %> do
17
+ # This should return the minimal set of attributes required to create a valid
18
+ # <%= class_name %>. As you add validations to <%= class_name %>, be sure to
19
+ # adjust the attributes here as well.
20
+ let(:valid_attributes) {
21
+ skip("Add a hash of attributes valid for your model")
22
+ }
23
+
24
+ let(:invalid_attributes) {
25
+ skip("Add a hash of attributes invalid for your model")
26
+ }
27
+
28
+ # This should return the minimal set of values that should be in the headers
29
+ # in order to pass any filters (e.g. authentication) defined in
30
+ # <%= controller_class_name %>Controller, or in your router and rack
31
+ # middleware. Be sure to keep this updated too.
32
+ let(:valid_headers) {
33
+ {}
34
+ }
35
+
36
+ <% unless options[:singleton] -%>
37
+ describe "GET /index" do
38
+ it "renders a successful response" do
39
+ <%= class_name %>.create! valid_attributes
40
+ get <%= index_helper %>_url, headers: valid_headers, as: :json
41
+ expect(response).to be_successful
42
+ end
43
+ end
44
+ <% end -%>
45
+
46
+ describe "GET /show" do
47
+ it "renders a successful response" do
48
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
49
+ get <%= show_helper %>, as: :json
50
+ expect(response).to be_successful
51
+ end
52
+ end
53
+
54
+ describe "POST /create" do
55
+ context "with valid parameters" do
56
+ it "creates a new <%= class_name %>" do
57
+ expect {
58
+ post <%= index_helper %>_url,
59
+ params: { <%= singular_table_name %>: valid_attributes }, headers: valid_headers, as: :json
60
+ }.to change(<%= class_name %>, :count).by(1)
61
+ end
62
+
63
+ it "renders a JSON response with the new <%= singular_table_name %>" do
64
+ post <%= index_helper %>_url,
65
+ params: { <%= singular_table_name %>: valid_attributes }, headers: valid_headers, as: :json
66
+ expect(response).to have_http_status(:created)
67
+ expect(response.content_type).to match(a_string_including("application/json"))
68
+ end
69
+ end
70
+
71
+ context "with invalid parameters" do
72
+ it "does not create a new <%= class_name %>" do
73
+ expect {
74
+ post <%= index_helper %>_url,
75
+ params: { <%= singular_table_name %>: invalid_attributes }, as: :json
76
+ }.to change(<%= class_name %>, :count).by(0)
77
+ end
78
+
79
+ it "renders a JSON response with errors for the new <%= singular_table_name %>" do
80
+ post <%= index_helper %>_url,
81
+ params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
82
+ expect(response).to have_http_status(:unprocessable_entity)
83
+ expect(response.content_type).to match(a_string_including("application/json"))
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "PATCH /update" do
89
+ context "with valid parameters" do
90
+ let(:new_attributes) {
91
+ skip("Add a hash of attributes valid for your model")
92
+ }
93
+
94
+ it "updates the requested <%= singular_table_name %>" do
95
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
96
+ patch <%= show_helper %>,
97
+ params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
98
+ <%= file_name %>.reload
99
+ skip("Add assertions for updated state")
100
+ end
101
+
102
+ it "renders a JSON response with the <%= singular_table_name %>" do
103
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
104
+ patch <%= show_helper %>,
105
+ params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
106
+ expect(response).to have_http_status(:ok)
107
+ expect(response.content_type).to match(a_string_including("application/json"))
108
+ end
109
+ end
110
+
111
+ context "with invalid parameters" do
112
+ it "renders a JSON response with errors for the <%= singular_table_name %>" do
113
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
114
+ patch <%= show_helper %>,
115
+ params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
116
+ expect(response).to have_http_status(:unprocessable_entity)
117
+ expect(response.content_type).to match(a_string_including("application/json"))
118
+ end
119
+ end
120
+ end
121
+
122
+ describe "DELETE /destroy" do
123
+ it "destroys the requested <%= singular_table_name %>" do
124
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
125
+ expect {
126
+ delete <%= show_helper %>, headers: valid_headers, as: :json
127
+ }.to change(<%= class_name %>, :count).by(-1)
128
+ end
129
+ end
130
+ end
131
+ <% end -%>