rspec-rails 2.14.2 → 3.9.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 (99) 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 +2 -4
  6. data/Changelog.md +592 -34
  7. data/{License.txt → LICENSE.md} +5 -2
  8. data/README.md +290 -369
  9. data/lib/generators/rspec/controller/controller_generator.rb +1 -0
  10. data/lib/generators/rspec/controller/templates/controller_spec.rb +5 -5
  11. data/lib/generators/rspec/controller/templates/view_spec.rb +2 -2
  12. data/lib/generators/rspec/feature/feature_generator.rb +29 -0
  13. data/lib/generators/rspec/feature/templates/feature_singular_spec.rb +5 -0
  14. data/lib/generators/rspec/feature/templates/feature_spec.rb +5 -0
  15. data/lib/generators/rspec/generators/generator_generator.rb +24 -0
  16. data/lib/generators/rspec/generators/templates/generator_spec.rb +6 -0
  17. data/lib/generators/rspec/helper/helper_generator.rb +1 -0
  18. data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
  19. data/lib/generators/rspec/install/install_generator.rb +44 -5
  20. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +78 -0
  21. data/lib/generators/rspec/integration/integration_generator.rb +8 -13
  22. data/lib/generators/rspec/integration/templates/request_spec.rb +4 -9
  23. data/lib/generators/rspec/job/job_generator.rb +12 -0
  24. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  25. data/lib/generators/rspec/mailer/mailer_generator.rb +7 -0
  26. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +7 -7
  27. data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
  28. data/lib/generators/rspec/model/model_generator.rb +19 -5
  29. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  30. data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
  31. data/lib/generators/rspec/observer/observer_generator.rb +1 -0
  32. data/lib/generators/rspec/observer/templates/observer_spec.rb +2 -2
  33. data/lib/generators/rspec/request/request_generator.rb +10 -0
  34. data/lib/generators/rspec/scaffold/scaffold_generator.rb +68 -138
  35. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +165 -0
  36. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +98 -73
  37. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +9 -13
  38. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -10
  39. data/lib/generators/rspec/scaffold/templates/new_spec.rb +10 -14
  40. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +21 -12
  41. data/lib/generators/rspec/scaffold/templates/show_spec.rb +4 -11
  42. data/lib/generators/rspec/system/system_generator.rb +26 -0
  43. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  44. data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
  45. data/lib/generators/rspec/view/view_generator.rb +1 -0
  46. data/lib/generators/rspec.rb +20 -6
  47. data/lib/rspec/rails/active_record.rb +25 -0
  48. data/lib/rspec/rails/adapters.rb +104 -37
  49. data/lib/rspec/rails/configuration.rb +148 -0
  50. data/lib/rspec/rails/example/controller_example_group.rb +188 -138
  51. data/lib/rspec/rails/example/feature_example_group.rb +63 -20
  52. data/lib/rspec/rails/example/helper_example_group.rb +35 -26
  53. data/lib/rspec/rails/example/job_example_group.rb +23 -0
  54. data/lib/rspec/rails/example/mailer_example_group.rb +30 -14
  55. data/lib/rspec/rails/example/model_example_group.rb +8 -7
  56. data/lib/rspec/rails/example/rails_example_group.rb +3 -1
  57. data/lib/rspec/rails/example/request_example_group.rb +23 -16
  58. data/lib/rspec/rails/example/routing_example_group.rb +49 -40
  59. data/lib/rspec/rails/example/system_example_group.rb +108 -0
  60. data/lib/rspec/rails/example/view_example_group.rb +168 -135
  61. data/lib/rspec/rails/example.rb +2 -33
  62. data/lib/rspec/rails/extensions/active_record/proxy.rb +0 -1
  63. data/lib/rspec/rails/extensions.rb +0 -1
  64. data/lib/rspec/rails/feature_check.rb +64 -0
  65. data/lib/rspec/rails/file_fixture_support.rb +17 -0
  66. data/lib/rspec/rails/fixture_file_upload_support.rb +40 -0
  67. data/lib/rspec/rails/fixture_support.rb +32 -13
  68. data/lib/rspec/rails/matchers/active_job.rb +317 -0
  69. data/lib/rspec/rails/matchers/base_matcher.rb +184 -0
  70. data/lib/rspec/rails/matchers/be_a_new.rb +69 -62
  71. data/lib/rspec/rails/matchers/be_new_record.rb +24 -21
  72. data/lib/rspec/rails/matchers/be_valid.rb +42 -33
  73. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +174 -0
  74. data/lib/rspec/rails/matchers/have_http_status.rb +381 -0
  75. data/lib/rspec/rails/matchers/have_rendered.rb +54 -31
  76. data/lib/rspec/rails/matchers/redirect_to.rb +30 -29
  77. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  78. data/lib/rspec/rails/matchers/routing_matchers.rb +107 -93
  79. data/lib/rspec/rails/matchers.rb +13 -14
  80. data/lib/rspec/rails/tasks/rspec.rake +1 -1
  81. data/lib/rspec/rails/vendor/capybara.rb +10 -4
  82. data/lib/rspec/rails/version.rb +3 -1
  83. data/lib/rspec/rails/view_assigns.rb +18 -18
  84. data/lib/rspec/rails/view_path_builder.rb +29 -0
  85. data/lib/rspec/rails/view_rendering.rb +89 -63
  86. data/lib/rspec/rails/view_spec_methods.rb +56 -0
  87. data/lib/rspec/rails.rb +10 -10
  88. data/lib/rspec-rails.rb +66 -1
  89. data.tar.gz.sig +0 -0
  90. metadata +92 -77
  91. metadata.gz.sig +0 -0
  92. data/lib/autotest/rails_rspec2.rb +0 -85
  93. data/lib/generators/rspec/install/templates/.rspec +0 -1
  94. data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +0 -49
  95. data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
  96. data/lib/rspec/rails/matchers/have_extension.rb +0 -36
  97. data/lib/rspec/rails/mocks.rb +0 -274
  98. data/lib/rspec/rails/module_inclusion.rb +0 -19
  99. data/lib/rspec/rails/vendor/webrat.rb +0 -33
@@ -3,6 +3,7 @@ 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
9
  source_paths << File.expand_path("../../helper/templates", __FILE__)
@@ -11,22 +12,35 @@ module Rspec
11
12
  class_option :orm, :desc => "ORM used to generate the controller"
12
13
  class_option :template_engine, :desc => "Template engine to generate view files"
13
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"
14
16
 
15
17
  class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
16
18
  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
19
  class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
20
20
  class_option :routing_specs, :type => :boolean, :default => true, :desc => "Generate routing specs"
21
21
 
22
+ def initialize(*args, &blk)
23
+ @generator_args = args.first
24
+ super(*args, &blk)
25
+ end
26
+
22
27
  def generate_controller_spec
23
28
  return unless options[:controller_specs]
24
29
 
25
- template 'controller_spec.rb',
26
- File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
30
+ template_file = File.join(
31
+ 'spec/controllers',
32
+ controller_class_path,
33
+ "#{controller_file_name}_controller_spec.rb"
34
+ )
35
+ if options[:api]
36
+ template 'api_controller_spec.rb', template_file
37
+ else
38
+ template 'controller_spec.rb', template_file
39
+ end
27
40
  end
28
41
 
29
42
  def generate_view_specs
43
+ return if options[:api]
30
44
  return unless options[:view_specs] && options[:template_engine]
31
45
 
32
46
  copy_view :edit
@@ -38,155 +52,71 @@ module Rspec
38
52
  def generate_routing_spec
39
53
  return unless options[:routing_specs]
40
54
 
41
- template 'routing_spec.rb',
42
- File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")
55
+ template_file = File.join(
56
+ 'spec/routing',
57
+ controller_class_path,
58
+ "#{controller_file_name}_routing_spec.rb"
59
+ )
60
+ template 'routing_spec.rb', template_file
43
61
  end
44
62
 
45
63
  hook_for :integration_tool, :as => :integration
46
64
 
47
- protected
65
+ protected
48
66
 
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
67
+ attr_reader :generator_args
54
68
 
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
89
-
90
- def formatted_hash(hash)
91
- formatted = hash.inspect
92
- formatted.gsub!("{", "{ ")
93
- formatted.gsub!("}", " }")
94
- formatted.gsub!("=>", " => ")
95
- formatted
96
- end
97
-
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
102
-
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
107
-
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
69
+ def copy_view(view)
70
+ template "#{view}_spec.rb",
71
+ File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
72
+ end
114
73
 
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
74
+ # support for namespaced-resources
75
+ def ns_file_name
76
+ return file_name if ns_parts.empty?
77
+ "#{ns_prefix.map(&:underscore).join('/')}_#{ns_suffix.singularize.underscore}"
78
+ end
136
79
 
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
80
+ # support for namespaced-resources
81
+ def ns_table_name
82
+ return table_name if ns_parts.empty?
83
+ "#{ns_prefix.map(&:underscore).join('/')}/#{ns_suffix.tableize}"
84
+ end
150
85
 
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
86
+ def ns_parts
87
+ @ns_parts ||= begin
88
+ parts = generator_args[0].split(/\/|::/)
89
+ parts.size > 1 ? parts : []
90
+ end
91
+ end
164
92
 
165
- def stub_or_should_chain(mode, chain)
166
- receiver, method = chain.split(".")
167
- method.gsub!(/\((.*?)\)/, '')
93
+ def ns_prefix
94
+ @ns_prefix ||= ns_parts[0..-2]
95
+ end
168
96
 
169
- response = "#{receiver}.#{mode}(:#{method})"
170
- response << ".with(#{$1})" unless $1.blank?
171
- response
172
- end
97
+ def ns_suffix
98
+ @ns_suffix ||= ns_parts[-1]
99
+ end
173
100
 
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
101
+ def value_for(attribute)
102
+ raw_value_for(attribute).inspect
103
+ end
185
104
 
186
- def banner
187
- self.class.banner
105
+ def raw_value_for(attribute)
106
+ case attribute.type
107
+ when :string
108
+ attribute.name.titleize
109
+ when :integer, :float
110
+ @attribute_id_map ||= {}
111
+ @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next + attribute.default
112
+ else
113
+ attribute.default
188
114
  end
115
+ end
189
116
 
117
+ def banner
118
+ self.class.banner
119
+ end
190
120
  end
191
121
  end
192
122
  end
@@ -0,0 +1,165 @@
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
+ <% if RUBY_VERSION < '1.9.3' -%>
50
+ get :index, {}, valid_session
51
+ <% else -%>
52
+ get :index, params: {}, session: valid_session
53
+ <% end -%>
54
+ expect(response).to be_successful
55
+ end
56
+ end
57
+
58
+ <% end -%>
59
+ describe "GET #show" do
60
+ it "returns a success response" do
61
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
62
+ <% if RUBY_VERSION < '1.9.3' -%>
63
+ get :show, {:id => <%= file_name %>.to_param}, valid_session
64
+ <% else -%>
65
+ get :show, params: {id: <%= file_name %>.to_param}, session: valid_session
66
+ <% end -%>
67
+ expect(response).to be_successful
68
+ end
69
+ end
70
+
71
+ describe "POST #create" do
72
+ context "with valid params" do
73
+ it "creates a new <%= class_name %>" do
74
+ expect {
75
+ <% if RUBY_VERSION < '1.9.3' -%>
76
+ post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
77
+ <% else -%>
78
+ post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
79
+ <% end -%>
80
+ }.to change(<%= class_name %>, :count).by(1)
81
+ end
82
+
83
+ it "renders a JSON response with the new <%= ns_file_name %>" do
84
+ <% if RUBY_VERSION < '1.9.3' -%>
85
+ post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
86
+ <% else %>
87
+ post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
88
+ <% end -%>
89
+ expect(response).to have_http_status(:created)
90
+ expect(response.content_type).to eq('application/json')
91
+ expect(response.location).to eq(<%= ns_file_name %>_url(<%= class_name %>.last))
92
+ end
93
+ end
94
+
95
+ context "with invalid params" do
96
+ it "renders a JSON response with errors for the new <%= ns_file_name %>" do
97
+ <% if RUBY_VERSION < '1.9.3' -%>
98
+ post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session
99
+ <% else %>
100
+ post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
101
+ <% end -%>
102
+ expect(response).to have_http_status(:unprocessable_entity)
103
+ expect(response.content_type).to eq('application/json')
104
+ end
105
+ end
106
+ end
107
+
108
+ describe "PUT #update" do
109
+ context "with valid params" do
110
+ let(:new_attributes) {
111
+ skip("Add a hash of attributes valid for your model")
112
+ }
113
+
114
+ it "updates the requested <%= ns_file_name %>" do
115
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
116
+ <% if RUBY_VERSION < '1.9.3' -%>
117
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => new_attributes}, valid_session
118
+ <% else -%>
119
+ put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
120
+ <% end -%>
121
+ <%= file_name %>.reload
122
+ skip("Add assertions for updated state")
123
+ end
124
+
125
+ it "renders a JSON response with the <%= ns_file_name %>" do
126
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
127
+ <% if RUBY_VERSION < '1.9.3' -%>
128
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
129
+ <% else %>
130
+ put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
131
+ <% end -%>
132
+ expect(response).to have_http_status(:ok)
133
+ expect(response.content_type).to eq('application/json')
134
+ end
135
+ end
136
+
137
+ context "with invalid params" do
138
+ it "renders a JSON response with errors for the <%= ns_file_name %>" do
139
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
140
+ <% if RUBY_VERSION < '1.9.3' -%>
141
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session
142
+ <% else %>
143
+ put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
144
+ <% end -%>
145
+ expect(response).to have_http_status(:unprocessable_entity)
146
+ expect(response.content_type).to eq('application/json')
147
+ end
148
+ end
149
+ end
150
+
151
+ describe "DELETE #destroy" do
152
+ it "destroys the requested <%= ns_file_name %>" do
153
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
154
+ expect {
155
+ <% if RUBY_VERSION < '1.9.3' -%>
156
+ delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
157
+ <% else -%>
158
+ delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
159
+ <% end -%>
160
+ }.to change(<%= class_name %>, :count).by(-1)
161
+ end
162
+ end
163
+
164
+ end
165
+ <% end -%>
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  # This spec was generated by rspec-rails when you ran the scaffold generator.
4
4
  # It demonstrates how one might use RSpec to specify the controller code that
@@ -17,14 +17,25 @@ require 'spec_helper'
17
17
  # is no simpler way to get a handle on the object needed for the example.
18
18
  # Message expectations are only used when there is no simpler way to specify
19
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.
20
25
 
21
26
  <% module_namespacing do -%>
22
- describe <%= controller_class_name %>Controller do
27
+ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:controller) %> do
23
28
 
24
29
  # This should return the minimal set of attributes required to create a valid
25
30
  # <%= class_name %>. As you add validations to <%= class_name %>, be sure to
26
31
  # adjust the attributes here as well.
27
- let(:valid_attributes) { <%= formatted_hash(example_valid_attributes) %> }
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
+ }
28
39
 
29
40
  # This should return the minimal set of values that should be in the session
30
41
  # in order to pass any filters (e.g. authentication) defined in
@@ -32,135 +43,149 @@ describe <%= controller_class_name %>Controller do
32
43
  let(:valid_session) { {} }
33
44
 
34
45
  <% unless options[:singleton] -%>
35
- describe "GET index" do
36
- it "assigns all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
37
- <%= file_name %> = <%= class_name %>.create! valid_attributes
46
+ describe "GET #index" do
47
+ it "returns a success response" do
48
+ <%= class_name %>.create! valid_attributes
49
+ <% if Rails::VERSION::STRING < '5.0' -%>
38
50
  get :index, {}, valid_session
39
- assigns(:<%= table_name %>).should eq([<%= file_name %>])
51
+ <% else -%>
52
+ get :index, params: {}, session: valid_session
53
+ <% end -%>
54
+ expect(response).to be_successful
40
55
  end
41
56
  end
42
57
 
43
58
  <% end -%>
44
- describe "GET show" do
45
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
59
+ describe "GET #show" do
60
+ it "returns a success response" do
46
61
  <%= file_name %> = <%= class_name %>.create! valid_attributes
62
+ <% if Rails::VERSION::STRING < '5.0' -%>
47
63
  get :show, {:id => <%= file_name %>.to_param}, valid_session
48
- assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
64
+ <% else -%>
65
+ get :show, params: {id: <%= file_name %>.to_param}, session: valid_session
66
+ <% end -%>
67
+ expect(response).to be_successful
49
68
  end
50
69
  end
51
70
 
52
- describe "GET new" do
53
- it "assigns a new <%= ns_file_name %> as @<%= ns_file_name %>" do
71
+ describe "GET #new" do
72
+ it "returns a success response" do
73
+ <% if Rails::VERSION::STRING < '5.0' -%>
54
74
  get :new, {}, valid_session
55
- assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
75
+ <% else -%>
76
+ get :new, params: {}, session: valid_session
77
+ <% end -%>
78
+ expect(response).to be_successful
56
79
  end
57
80
  end
58
81
 
59
- describe "GET edit" do
60
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
82
+ describe "GET #edit" do
83
+ it "returns a success response" do
61
84
  <%= file_name %> = <%= class_name %>.create! valid_attributes
85
+ <% if Rails::VERSION::STRING < '5.0' -%>
62
86
  get :edit, {:id => <%= file_name %>.to_param}, valid_session
63
- assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
87
+ <% else -%>
88
+ get :edit, params: {id: <%= file_name %>.to_param}, session: valid_session
89
+ <% end -%>
90
+ expect(response).to be_successful
64
91
  end
65
92
  end
66
93
 
67
- describe "POST create" do
68
- describe "with valid params" do
94
+ describe "POST #create" do
95
+ context "with valid params" do
69
96
  it "creates a new <%= class_name %>" do
70
97
  expect {
98
+ <% if Rails::VERSION::STRING < '5.0' -%>
71
99
  post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
100
+ <% else -%>
101
+ post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
102
+ <% end -%>
72
103
  }.to change(<%= class_name %>, :count).by(1)
73
104
  end
74
105
 
75
- it "assigns a newly created <%= ns_file_name %> as @<%= ns_file_name %>" do
76
- post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
77
- assigns(:<%= ns_file_name %>).should be_a(<%= class_name %>)
78
- assigns(:<%= ns_file_name %>).should be_persisted
79
- end
80
-
81
106
  it "redirects to the created <%= ns_file_name %>" do
107
+ <% if Rails::VERSION::STRING < '5.0' -%>
82
108
  post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
83
- response.should redirect_to(<%= class_name %>.last)
109
+ <% else -%>
110
+ post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
111
+ <% end -%>
112
+ expect(response).to redirect_to(<%= class_name %>.last)
84
113
  end
85
114
  end
86
115
 
87
- describe "with invalid params" do
88
- it "assigns a newly created but unsaved <%= ns_file_name %> as @<%= ns_file_name %>" do
89
- # Trigger the behavior that occurs when invalid params are submitted
90
- <%= class_name %>.any_instance.stub(:save).and_return(false)
91
- post :create, {:<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
92
- assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
93
- end
94
-
95
- it "re-renders the 'new' template" do
96
- # Trigger the behavior that occurs when invalid params are submitted
97
- <%= class_name %>.any_instance.stub(:save).and_return(false)
98
- post :create, {:<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
99
- response.should render_template("new")
116
+ context "with invalid params" do
117
+ it "returns a success response (i.e. to display the 'new' template)" do
118
+ <% if Rails::VERSION::STRING < '5.0' -%>
119
+ post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session
120
+ <% else -%>
121
+ post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
122
+ <% end -%>
123
+ expect(response).to be_successful
100
124
  end
101
125
  end
102
126
  end
103
127
 
104
- describe "PUT update" do
105
- describe "with valid params" do
106
- it "updates the requested <%= ns_file_name %>" do
107
- <%= file_name %> = <%= class_name %>.create! valid_attributes
108
- # Assuming there are no other <%= table_name %> in the database, this
109
- # specifies that the <%= class_name %> created on the previous line
110
- # receives the :update_attributes message with whatever params are
111
- # submitted in the request.
112
- <%- if ::Rails::VERSION::STRING >= '4' -%>
113
- <%= class_name %>.any_instance.should_receive(:update).with(<%= formatted_hash(example_params_for_update) %>)
114
- <%- else -%>
115
- <%= class_name %>.any_instance.should_receive(:update_attributes).with(<%= formatted_hash(example_params_for_update) %>)
116
- <%- end -%>
117
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_params_for_update) %>}, valid_session
118
- end
128
+ describe "PUT #update" do
129
+ context "with valid params" do
130
+ let(:new_attributes) {
131
+ skip("Add a hash of attributes valid for your model")
132
+ }
119
133
 
120
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
134
+ it "updates the requested <%= ns_file_name %>" do
121
135
  <%= file_name %> = <%= class_name %>.create! valid_attributes
122
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
123
- assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
136
+ <% if Rails::VERSION::STRING < '5.0' -%>
137
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => new_attributes}, valid_session
138
+ <% else -%>
139
+ put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
140
+ <% end -%>
141
+ <%= file_name %>.reload
142
+ skip("Add assertions for updated state")
124
143
  end
125
144
 
126
145
  it "redirects to the <%= ns_file_name %>" do
127
146
  <%= file_name %> = <%= class_name %>.create! valid_attributes
147
+ <% if Rails::VERSION::STRING < '5.0' -%>
128
148
  put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
129
- response.should redirect_to(<%= file_name %>)
149
+ <% else -%>
150
+ put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
151
+ <% end -%>
152
+ expect(response).to redirect_to(<%= file_name %>)
130
153
  end
131
154
  end
132
155
 
133
- describe "with invalid params" do
134
- it "assigns the <%= ns_file_name %> as @<%= ns_file_name %>" do
135
- <%= file_name %> = <%= class_name %>.create! valid_attributes
136
- # Trigger the behavior that occurs when invalid params are submitted
137
- <%= class_name %>.any_instance.stub(:save).and_return(false)
138
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
139
- assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
140
- end
141
-
142
- it "re-renders the 'edit' template" do
156
+ context "with invalid params" do
157
+ it "returns a success response (i.e. to display the 'edit' template)" do
143
158
  <%= file_name %> = <%= class_name %>.create! valid_attributes
144
- # Trigger the behavior that occurs when invalid params are submitted
145
- <%= class_name %>.any_instance.stub(:save).and_return(false)
146
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
147
- response.should render_template("edit")
159
+ <% if Rails::VERSION::STRING < '5.0' -%>
160
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session
161
+ <% else -%>
162
+ put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
163
+ <% end -%>
164
+ expect(response).to be_successful
148
165
  end
149
166
  end
150
167
  end
151
168
 
152
- describe "DELETE destroy" do
169
+ describe "DELETE #destroy" do
153
170
  it "destroys the requested <%= ns_file_name %>" do
154
171
  <%= file_name %> = <%= class_name %>.create! valid_attributes
155
172
  expect {
173
+ <% if Rails::VERSION::STRING < '5.0' -%>
156
174
  delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
175
+ <% else -%>
176
+ delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
177
+ <% end -%>
157
178
  }.to change(<%= class_name %>, :count).by(-1)
158
179
  end
159
180
 
160
181
  it "redirects to the <%= table_name %> list" do
161
182
  <%= file_name %> = <%= class_name %>.create! valid_attributes
183
+ <% if Rails::VERSION::STRING < '5.0' -%>
162
184
  delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
163
- response.should redirect_to(<%= index_helper %>_url)
185
+ <% else -%>
186
+ delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
187
+ <% end -%>
188
+ expect(response).to redirect_to(<%= index_helper %>_url)
164
189
  end
165
190
  end
166
191