rspec-rails 3.0.2 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +3 -1
  5. data/Capybara.md +6 -55
  6. data/Changelog.md +805 -47
  7. data/{License.txt → LICENSE.md} +5 -3
  8. data/README.md +278 -444
  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} +1 -1
  11. data/lib/generators/rspec/controller/controller_generator.rb +24 -7
  12. data/lib/generators/rspec/controller/templates/controller_spec.rb +3 -3
  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 +1 -1
  16. data/lib/generators/rspec/feature/feature_generator.rb +15 -2
  17. data/lib/generators/rspec/feature/templates/feature_singular_spec.rb +5 -0
  18. data/lib/generators/rspec/feature/templates/feature_spec.rb +1 -1
  19. data/lib/generators/rspec/generator/generator_generator.rb +24 -0
  20. data/lib/generators/rspec/generator/templates/generator_spec.rb +5 -0
  21. data/lib/generators/rspec/helper/helper_generator.rb +2 -2
  22. data/lib/generators/rspec/helper/templates/helper_spec.rb +1 -1
  23. data/lib/generators/rspec/install/install_generator.rb +41 -7
  24. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +63 -22
  25. data/lib/generators/rspec/job/job_generator.rb +13 -0
  26. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  27. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  28. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  29. data/lib/generators/rspec/mailer/mailer_generator.rb +12 -3
  30. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
  31. data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
  32. data/lib/generators/rspec/model/model_generator.rb +20 -6
  33. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  34. data/lib/generators/rspec/model/templates/model_spec.rb +1 -1
  35. data/lib/generators/rspec/request/request_generator.rb +17 -0
  36. data/lib/generators/rspec/request/templates/request_spec.rb +10 -0
  37. data/lib/generators/rspec/scaffold/scaffold_generator.rb +90 -113
  38. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +129 -0
  39. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  40. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +46 -64
  41. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +11 -7
  42. data/lib/generators/rspec/scaffold/templates/index_spec.rb +4 -3
  43. data/lib/generators/rspec/scaffold/templates/new_spec.rb +4 -4
  44. data/lib/generators/rspec/scaffold/templates/request_spec.rb +138 -0
  45. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +18 -11
  46. data/lib/generators/rspec/scaffold/templates/show_spec.rb +3 -3
  47. data/lib/generators/rspec/system/system_generator.rb +24 -0
  48. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  49. data/lib/generators/rspec/view/templates/view_spec.rb +1 -1
  50. data/lib/generators/rspec/view/view_generator.rb +4 -4
  51. data/lib/generators/rspec.rb +30 -11
  52. data/lib/rspec/rails/active_record.rb +25 -0
  53. data/lib/rspec/rails/adapters.rb +46 -29
  54. data/lib/rspec/rails/configuration.rb +165 -41
  55. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  56. data/lib/rspec/rails/example/controller_example_group.rb +185 -149
  57. data/lib/rspec/rails/example/feature_example_group.rb +43 -23
  58. data/lib/rspec/rails/example/helper_example_group.rb +28 -25
  59. data/lib/rspec/rails/example/job_example_group.rb +23 -0
  60. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  61. data/lib/rspec/rails/example/mailer_example_group.rb +27 -22
  62. data/lib/rspec/rails/example/model_example_group.rb +9 -6
  63. data/lib/rspec/rails/example/rails_example_group.rb +9 -2
  64. data/lib/rspec/rails/example/request_example_group.rb +21 -17
  65. data/lib/rspec/rails/example/routing_example_group.rb +47 -39
  66. data/lib/rspec/rails/example/system_example_group.rb +180 -0
  67. data/lib/rspec/rails/example/view_example_group.rb +179 -134
  68. data/lib/rspec/rails/example.rb +4 -0
  69. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -11
  70. data/lib/rspec/rails/feature_check.rb +51 -0
  71. data/lib/rspec/rails/file_fixture_support.rb +18 -0
  72. data/lib/rspec/rails/fixture_file_upload_support.rb +45 -0
  73. data/lib/rspec/rails/fixture_support.rb +70 -14
  74. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +180 -0
  75. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  76. data/lib/rspec/rails/matchers/action_cable.rb +70 -0
  77. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  78. data/lib/rspec/rails/matchers/active_job.rb +526 -0
  79. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  80. data/lib/rspec/rails/matchers/be_a_new.rb +70 -64
  81. data/lib/rspec/rails/matchers/be_new_record.rb +25 -20
  82. data/lib/rspec/rails/matchers/be_valid.rb +39 -34
  83. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +259 -0
  84. data/lib/rspec/rails/matchers/have_http_status.rb +359 -333
  85. data/lib/rspec/rails/matchers/have_rendered.rb +55 -32
  86. data/lib/rspec/rails/matchers/redirect_to.rb +30 -27
  87. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  88. data/lib/rspec/rails/matchers/routing_matchers.rb +107 -101
  89. data/lib/rspec/rails/matchers/send_email.rb +122 -0
  90. data/lib/rspec/rails/matchers.rb +21 -12
  91. data/lib/rspec/rails/tasks/rspec.rake +9 -17
  92. data/lib/rspec/rails/vendor/capybara.rb +10 -11
  93. data/lib/rspec/rails/version.rb +1 -1
  94. data/lib/rspec/rails/view_assigns.rb +1 -20
  95. data/lib/rspec/rails/view_path_builder.rb +29 -0
  96. data/lib/rspec/rails/view_rendering.rb +89 -27
  97. data/lib/rspec/rails/view_spec_methods.rb +56 -0
  98. data/lib/rspec/rails.rb +9 -1
  99. data/lib/rspec-rails.rb +83 -3
  100. data.tar.gz.sig +0 -0
  101. metadata +108 -78
  102. metadata.gz.sig +3 -2
  103. data/lib/generators/rspec/integration/integration_generator.rb +0 -17
  104. data/lib/generators/rspec/integration/templates/request_spec.rb +0 -10
  105. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
@@ -6,26 +6,47 @@ module Rspec
6
6
  # @private
7
7
  class ScaffoldGenerator < Base
8
8
  include ::Rails::Generators::ResourceHelpers
9
- source_paths << File.expand_path("../../helper/templates", __FILE__)
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
-
16
- class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
17
- class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
18
- class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
19
- 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
20
27
 
21
28
  def generate_controller_spec
22
29
  return unless options[:controller_specs]
23
30
 
24
- template 'controller_spec.rb',
25
- 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
26
46
  end
27
47
 
28
48
  def generate_view_specs
49
+ return if options[:api]
29
50
  return unless options[:view_specs] && options[:template_engine]
30
51
 
31
52
  copy_view :edit
@@ -37,123 +58,79 @@ module Rspec
37
58
  def generate_routing_spec
38
59
  return unless options[:routing_specs]
39
60
 
40
- template 'routing_spec.rb',
41
- 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
42
67
  end
43
68
 
44
- hook_for :integration_tool, :as => :integration
69
+ protected
45
70
 
46
- protected
71
+ attr_reader :generator_args
47
72
 
48
- def copy_view(view)
49
- template "#{view}_spec.rb",
50
- File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
51
- end
52
-
53
- def formatted_hash(hash)
54
- formatted = hash.inspect
55
- formatted.gsub!("{", "{ ")
56
- formatted.gsub!("}", " }")
57
- formatted.gsub!("=>", " => ")
58
- formatted
59
- 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
60
77
 
61
- # support for namespaced-resources
62
- def ns_file_name
63
- ns_parts.empty? ? file_name : "#{ns_parts[0].underscore}_#{ns_parts[1].singularize.underscore}"
64
- end
78
+ # support for namespaced-resources
79
+ def ns_file_name
80
+ return file_name if ns_parts.empty?
65
81
 
66
- # support for namespaced-resources
67
- def ns_table_name
68
- ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}"
69
- end
82
+ "#{ns_prefix.map(&:underscore).join('/')}_#{ns_suffix.singularize.underscore}"
83
+ end
70
84
 
71
- def ns_parts
72
- @ns_parts ||= begin
73
- matches = ARGV[0].to_s.match(/\A(\w+)(?:\/|::)(\w+)/)
74
- matches ? [matches[1], matches[2]] : []
75
- end
76
- end
85
+ # support for namespaced-resources
86
+ def ns_table_name
87
+ return table_name if ns_parts.empty?
77
88
 
78
- # Returns the name of the mock. For example, if the file name is user,
79
- # it returns mock_user.
80
- #
81
- # If a hash is given, it uses the hash key as the ORM method and the
82
- # value as response. So, for ActiveRecord and file name "User":
83
- #
84
- # mock_file_name(:save => true)
85
- # #=> mock_user(:save => true)
86
- #
87
- # If another ORM is being used and another method instead of save is
88
- # called, it will be the one used.
89
- #
90
- def mock_file_name(hash=nil)
91
- if hash
92
- method, and_return = hash.to_a.first
93
- method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '')
94
- "mock_#{ns_file_name}(:#{method} => #{and_return})"
95
- else
96
- "mock_#{ns_file_name}"
97
- end
98
- end
89
+ "#{ns_prefix.map(&:underscore).join('/')}/#{ns_suffix.tableize}"
90
+ end
99
91
 
100
- # Receives the ORM chain and convert to expects. For ActiveRecord:
101
- #
102
- # should! orm_class.find(User, "37")
103
- # #=> User.should_receive(:find).with(37)
104
- #
105
- # For Datamapper:
106
- #
107
- # should! orm_class.find(User, "37")
108
- # #=> User.should_receive(:get).with(37)
109
- #
110
- def should_receive(chain)
111
- stub_or_should_chain(:should_receive, chain)
112
- end
92
+ def ns_parts
93
+ @ns_parts ||= begin
94
+ parts = generator_args[0].split(/\/|::/)
95
+ parts.size > 1 ? parts : []
96
+ end
97
+ end
113
98
 
114
- # Receives the ORM chain and convert to stub. For ActiveRecord:
115
- #
116
- # stub orm_class.find(User, "37")
117
- # #=> User.stub(:find).with(37)
118
- #
119
- # For Datamapper:
120
- #
121
- # stub orm_class.find(User, "37")
122
- # #=> User.stub(:get).with(37)
123
- #
124
- def stub(chain)
125
- stub_or_should_chain(:stub, chain)
126
- end
99
+ def ns_prefix
100
+ @ns_prefix ||= ns_parts[0..-2]
101
+ end
127
102
 
128
- def stub_or_should_chain(mode, chain)
129
- receiver, method = chain.split(".")
130
- method.gsub!(/\((.*?)\)/, '')
103
+ def ns_suffix
104
+ @ns_suffix ||= ns_parts[-1]
105
+ end
131
106
 
132
- response = "#{receiver}.#{mode}(:#{method})"
133
- response << ".with(#{$1})" unless $1.blank?
134
- response
135
- end
107
+ def value_for(attribute)
108
+ raw_value_for(attribute).inspect
109
+ end
136
110
 
137
- def value_for(attribute)
138
- raw_value_for(attribute).inspect
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
139
120
  end
121
+ end
140
122
 
141
- def raw_value_for(attribute)
142
- case attribute.type
143
- when :string
144
- attribute.name.titleize
145
- when :integer
146
- @attribute_id_map ||= {}
147
- @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next
148
- else
149
- attribute.default
150
- end
151
- end
123
+ def template_file(folder:, suffix: '')
124
+ target_path(folder, controller_class_path, "#{controller_file_name}#{suffix}_spec.rb")
125
+ end
152
126
 
153
- def banner
154
- self.class.banner
155
- end
127
+ def banner
128
+ self.class.banner
129
+ end
156
130
 
131
+ def show_helper(resource_name = file_name)
132
+ "#{singular_route_name}_url(#{resource_name})"
133
+ end
157
134
  end
158
135
  end
159
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 -%>