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
@@ -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,116 @@ 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
38
- get :index, {}, valid_session
39
- assigns(:<%= table_name %>).should eq([<%= file_name %>])
46
+ describe "GET #index" do
47
+ it "returns a success response" do
48
+ <%= class_name %>.create! valid_attributes
49
+ get :index, params: {}, session: valid_session
50
+ expect(response).to be_successful
40
51
  end
41
52
  end
42
53
 
43
54
  <% end -%>
44
- describe "GET show" do
45
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
55
+ describe "GET #show" do
56
+ it "returns a success response" do
46
57
  <%= file_name %> = <%= class_name %>.create! valid_attributes
47
- get :show, {:id => <%= file_name %>.to_param}, valid_session
48
- assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
58
+ get :show, params: {id: <%= file_name %>.to_param}, session: valid_session
59
+ expect(response).to be_successful
49
60
  end
50
61
  end
51
62
 
52
- describe "GET new" do
53
- it "assigns a new <%= ns_file_name %> as @<%= ns_file_name %>" do
54
- get :new, {}, valid_session
55
- assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
63
+ describe "GET #new" do
64
+ it "returns a success response" do
65
+ get :new, params: {}, session: valid_session
66
+ expect(response).to be_successful
56
67
  end
57
68
  end
58
69
 
59
- describe "GET edit" do
60
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
70
+ describe "GET #edit" do
71
+ it "returns a success response" do
61
72
  <%= file_name %> = <%= class_name %>.create! valid_attributes
62
- get :edit, {:id => <%= file_name %>.to_param}, valid_session
63
- assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
73
+ get :edit, params: {id: <%= file_name %>.to_param}, session: valid_session
74
+ expect(response).to be_successful
64
75
  end
65
76
  end
66
77
 
67
- describe "POST create" do
68
- describe "with valid params" do
78
+ describe "POST #create" do
79
+ context "with valid params" do
69
80
  it "creates a new <%= class_name %>" do
70
81
  expect {
71
- post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
82
+ post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
72
83
  }.to change(<%= class_name %>, :count).by(1)
73
84
  end
74
85
 
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
- it "redirects to the created <%= ns_file_name %>" do
82
- post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
83
- response.should redirect_to(<%= class_name %>.last)
86
+ it "redirects to the created <%= singular_table_name %>" do
87
+ post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
88
+ expect(response).to redirect_to(<%= class_name %>.last)
84
89
  end
85
90
  end
86
91
 
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 %>)
92
+ context "with invalid params" do
93
+ <% if Rails.version.to_f < 7.0 %>
94
+ it "returns a success response (i.e. to display the 'new' template)" do
95
+ post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
96
+ expect(response).to be_successful
93
97
  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")
98
+ <% else %>
99
+ it "renders a response with 422 status (i.e. to display the 'new' template)" do
100
+ post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
101
+ expect(response).to have_http_status(:unprocessable_entity)
100
102
  end
103
+ <% end %>
101
104
  end
102
105
  end
103
106
 
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
107
+ describe "PUT #update" do
108
+ context "with valid params" do
109
+ let(:new_attributes) {
110
+ skip("Add a hash of attributes valid for your model")
111
+ }
119
112
 
120
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
113
+ it "updates the requested <%= singular_table_name %>" do
121
114
  <%= 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 %>)
115
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
116
+ <%= file_name %>.reload
117
+ skip("Add assertions for updated state")
124
118
  end
125
119
 
126
- it "redirects to the <%= ns_file_name %>" do
120
+ it "redirects to the <%= singular_table_name %>" do
127
121
  <%= file_name %> = <%= class_name %>.create! valid_attributes
128
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
129
- response.should redirect_to(<%= file_name %>)
122
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
123
+ expect(response).to redirect_to(<%= file_name %>)
130
124
  end
131
125
  end
132
126
 
133
- describe "with invalid params" do
134
- it "assigns the <%= ns_file_name %> as @<%= ns_file_name %>" do
127
+ context "with invalid params" do
128
+ <% if Rails.version.to_f < 7.0 %>
129
+ it "returns a success response (i.e. to display the 'edit' template)" do
135
130
  <%= 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 %>)
131
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
132
+ expect(response).to be_successful
140
133
  end
141
-
142
- it "re-renders the 'edit' template" do
134
+ <% else %>
135
+ it "renders a response with 422 status (i.e. to display the 'edit' template)" do
143
136
  <%= 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")
137
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
138
+ expect(response).to have_http_status(:unprocessable_entity)
148
139
  end
140
+ <% end %>
149
141
  end
150
142
  end
151
143
 
152
- describe "DELETE destroy" do
153
- it "destroys the requested <%= ns_file_name %>" do
144
+ describe "DELETE #destroy" do
145
+ it "destroys the requested <%= singular_table_name %>" do
154
146
  <%= file_name %> = <%= class_name %>.create! valid_attributes
155
147
  expect {
156
- delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
148
+ delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
157
149
  }.to change(<%= class_name %>, :count).by(-1)
158
150
  end
159
151
 
160
152
  it "redirects to the <%= table_name %> list" do
161
153
  <%= file_name %> = <%= class_name %>.create! valid_attributes
162
- delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
163
- response.should redirect_to(<%= index_helper %>_url)
154
+ delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
155
+ expect(response).to redirect_to(<%= index_helper %>_url)
164
156
  end
165
157
  end
166
158
 
@@ -1,31 +1,27 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/edit" do
5
- before(:each) do
6
- @<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
4
+ RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do
5
+ let(:<%= singular_table_name %>) {
6
+ <%= class_name %>.create!(<%= ')' if output_attributes.empty? %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
- :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
8
+ <%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
10
- <%= output_attributes.empty? ? "" : " ))\n" -%>
10
+ <%= " )\n" unless output_attributes.empty? -%>
11
+ }
12
+
13
+ before(:each) do
14
+ assign(:<%= singular_table_name %>, <%= singular_table_name %>)
11
15
  end
12
16
 
13
17
  it "renders the edit <%= ns_file_name %> form" do
14
18
  render
15
19
 
16
- <% if webrat? -%>
17
- rendered.should have_selector("form", :action => <%= ns_file_name %>_path(@<%= ns_file_name %>), :method => "post") do |form|
20
+ assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(<%= singular_table_name %>), "post" do
18
21
  <% for attribute in output_attributes -%>
19
- form.should have_selector("<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]")
22
+ <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
23
+ assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"
20
24
  <% end -%>
21
25
  end
22
- <% else -%>
23
- # Run the generator again with the --webrat flag if you want to use webrat matchers
24
- assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= ns_file_name %>), "post" do
25
- <% for attribute in output_attributes -%>
26
- assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>[name=?]", "<%= ns_file_name %>[<%= attribute.name %>]"
27
- <% end -%>
28
- end
29
- <% end -%>
30
26
  end
31
27
  end
@@ -1,13 +1,13 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/index" do
4
+ RSpec.describe "<%= ns_table_name %>/index", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
6
  assign(:<%= table_name %>, [
7
7
  <% [1,2].each_with_index do |id, model_index| -%>
8
- stub_model(<%= class_name %><%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : ',' %>
8
+ <%= class_name %>.create!(<%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : '' %>
9
9
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
10
- :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
10
+ <%= attribute.name %>: <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
11
11
  <% end -%>
12
12
  <% if !output_attributes.empty? -%>
13
13
  <%= model_index == 1 ? ')' : '),' %>
@@ -18,15 +18,9 @@ describe "<%= ns_table_name %>/index" do
18
18
 
19
19
  it "renders a list of <%= ns_table_name %>" do
20
20
  render
21
- <% unless webrat? -%>
22
- # Run the generator again with the --webrat flag if you want to use webrat matchers
23
- <% end -%>
21
+ cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td'
24
22
  <% for attribute in output_attributes -%>
25
- <% if webrat? -%>
26
- rendered.should have_selector("tr>td", :content => <%= value_for(attribute) %>.to_s, :count => 2)
27
- <% else -%>
28
- assert_select "tr>td", :text => <%= value_for(attribute) %>.to_s, :count => 2
29
- <% end -%>
23
+ assert_select cell_selector, text: Regexp.new(<%= value_for(attribute) %>.to_s), count: 2
30
24
  <% end -%>
31
25
  end
32
26
  end
@@ -1,30 +1,22 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/new" do
4
+ RSpec.describe "<%= ns_table_name %>/new", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
- assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? ').as_new_record)' : ',' %>
6
+ assign(:<%= singular_table_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
- :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
8
+ <%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
10
- <%= !output_attributes.empty? ? " ).as_new_record)\n end" : " end" %>
10
+ <%= !output_attributes.empty? ? " ))\n end" : " end" %>
11
11
 
12
12
  it "renders new <%= ns_file_name %> form" do
13
13
  render
14
14
 
15
- <% if webrat? -%>
16
- rendered.should have_selector("form", :action => <%= table_name %>_path, :method => "post") do |form|
17
- <% for attribute in output_attributes -%>
18
- form.should have_selector("<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]")
19
- <% end -%>
20
- end
21
- <% else -%>
22
- # Run the generator again with the --webrat flag if you want to use webrat matchers
23
15
  assert_select "form[action=?][method=?]", <%= index_helper %>_path, "post" do
24
16
  <% for attribute in output_attributes -%>
25
- assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>[name=?]", "<%= ns_file_name %>[<%= attribute.name %>]"
17
+ <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
18
+ assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"
26
19
  <% end -%>
27
20
  end
28
- <% end -%>
29
21
  end
30
22
  end
@@ -0,0 +1,153 @@
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
+ <% if mountable_engine? -%>
18
+ include Engine.routes.url_helpers
19
+ <% end -%>
20
+
21
+ # This should return the minimal set of attributes required to create a valid
22
+ # <%= class_name %>. As you add validations to <%= class_name %>, be sure to
23
+ # adjust the attributes here as well.
24
+ let(:valid_attributes) {
25
+ skip("Add a hash of attributes valid for your model")
26
+ }
27
+
28
+ let(:invalid_attributes) {
29
+ skip("Add a hash of attributes invalid for your model")
30
+ }
31
+
32
+ <% unless options[:singleton] -%>
33
+ describe "GET /index" do
34
+ it "renders a successful response" do
35
+ <%= class_name %>.create! valid_attributes
36
+ get <%= index_helper %>_url
37
+ expect(response).to be_successful
38
+ end
39
+ end
40
+ <% end -%>
41
+
42
+ describe "GET /show" do
43
+ it "renders a successful response" do
44
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
45
+ get <%= show_helper %>
46
+ expect(response).to be_successful
47
+ end
48
+ end
49
+
50
+ describe "GET /new" do
51
+ it "renders a successful response" do
52
+ get <%= new_helper %>
53
+ expect(response).to be_successful
54
+ end
55
+ end
56
+
57
+ describe "GET /edit" do
58
+ it "renders a successful response" do
59
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
60
+ get <%= edit_helper %>
61
+ expect(response).to be_successful
62
+ end
63
+ end
64
+
65
+ describe "POST /create" do
66
+ context "with valid parameters" do
67
+ it "creates a new <%= class_name %>" do
68
+ expect {
69
+ post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }
70
+ }.to change(<%= class_name %>, :count).by(1)
71
+ end
72
+
73
+ it "redirects to the created <%= singular_table_name %>" do
74
+ post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }
75
+ expect(response).to redirect_to(<%= show_helper(class_name+".last") %>)
76
+ end
77
+ end
78
+
79
+ context "with invalid parameters" do
80
+ it "does not create a new <%= class_name %>" do
81
+ expect {
82
+ post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
83
+ }.to change(<%= class_name %>, :count).by(0)
84
+ end
85
+
86
+ <% if Rails.version.to_f < 7.0 %>
87
+ it "renders a successful response (i.e. to display the 'new' template)" do
88
+ post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
89
+ expect(response).to be_successful
90
+ end
91
+ <% else %>
92
+ it "renders a response with 422 status (i.e. to display the 'new' template)" do
93
+ post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
94
+ expect(response).to have_http_status(:unprocessable_entity)
95
+ end
96
+ <% end %>
97
+ end
98
+ end
99
+
100
+ describe "PATCH /update" do
101
+ context "with valid parameters" do
102
+ let(:new_attributes) {
103
+ skip("Add a hash of attributes valid for your model")
104
+ }
105
+
106
+ it "updates the requested <%= singular_table_name %>" do
107
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
108
+ patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
109
+ <%= file_name %>.reload
110
+ skip("Add assertions for updated state")
111
+ end
112
+
113
+ it "redirects to the <%= singular_table_name %>" do
114
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
115
+ patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
116
+ <%= file_name %>.reload
117
+ expect(response).to redirect_to(<%= singular_table_name %>_url(<%= file_name %>))
118
+ end
119
+ end
120
+
121
+ context "with invalid parameters" do
122
+ <% if Rails.version.to_f < 7.0 %>
123
+ it "renders a successful response (i.e. to display the 'edit' template)" do
124
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
125
+ patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
126
+ expect(response).to be_successful
127
+ end
128
+ <% else %>
129
+ it "renders a response with 422 status (i.e. to display the 'edit' template)" do
130
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
131
+ patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
132
+ expect(response).to have_http_status(:unprocessable_entity)
133
+ end
134
+ <% end %>
135
+ end
136
+ end
137
+
138
+ describe "DELETE /destroy" do
139
+ it "destroys the requested <%= singular_table_name %>" do
140
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
141
+ expect {
142
+ delete <%= show_helper %>
143
+ }.to change(<%= class_name %>, :count).by(-1)
144
+ end
145
+
146
+ it "redirects to the <%= table_name %> list" do
147
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
148
+ delete <%= show_helper %>
149
+ expect(response).to redirect_to(<%= index_helper %>_url)
150
+ end
151
+ end
152
+ end
153
+ <% end -%>
@@ -1,39 +1,46 @@
1
- require "spec_helper"
1
+ require "rails_helper"
2
2
 
3
3
  <% module_namespacing do -%>
4
- describe <%= controller_class_name %>Controller do
4
+ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:routing) %> do
5
5
  describe "routing" do
6
-
7
6
  <% unless options[:singleton] -%>
8
7
  it "routes to #index" do
9
- get("/<%= ns_table_name %>").should route_to("<%= ns_table_name %>#index")
8
+ expect(get: "/<%= ns_table_name %>").to route_to("<%= ns_table_name %>#index")
10
9
  end
11
10
 
12
11
  <% end -%>
12
+ <% unless options[:api] -%>
13
13
  it "routes to #new" do
14
- get("/<%= ns_table_name %>/new").should route_to("<%= ns_table_name %>#new")
14
+ expect(get: "/<%= ns_table_name %>/new").to route_to("<%= ns_table_name %>#new")
15
15
  end
16
16
 
17
+ <% end -%>
17
18
  it "routes to #show" do
18
- get("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#show", :id => "1")
19
+ expect(get: "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#show", id: "1")
19
20
  end
20
21
 
22
+ <% unless options[:api] -%>
21
23
  it "routes to #edit" do
22
- get("/<%= ns_table_name %>/1/edit").should route_to("<%= ns_table_name %>#edit", :id => "1")
24
+ expect(get: "/<%= ns_table_name %>/1/edit").to route_to("<%= ns_table_name %>#edit", id: "1")
23
25
  end
24
26
 
27
+ <% end -%>
28
+
25
29
  it "routes to #create" do
26
- post("/<%= ns_table_name %>").should route_to("<%= ns_table_name %>#create")
30
+ expect(post: "/<%= ns_table_name %>").to route_to("<%= ns_table_name %>#create")
27
31
  end
28
32
 
29
- it "routes to #update" do
30
- put("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#update", :id => "1")
33
+ it "routes to #update via PUT" do
34
+ expect(put: "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#update", id: "1")
31
35
  end
32
36
 
33
- it "routes to #destroy" do
34
- delete("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#destroy", :id => "1")
37
+ it "routes to #update via PATCH" do
38
+ expect(patch: "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#update", id: "1")
35
39
  end
36
40
 
41
+ it "routes to #destroy" do
42
+ expect(delete: "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#destroy", id: "1")
43
+ end
37
44
  end
38
45
  end
39
46
  <% end -%>
@@ -1,11 +1,11 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/show" do
4
+ RSpec.describe "<%= ns_table_name %>/show", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
- @<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
6
+ assign(:<%= singular_table_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
- :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
8
+ <%= attribute.name %>: <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
10
10
  <% if !output_attributes.empty? -%>
11
11
  ))
@@ -14,15 +14,8 @@ describe "<%= ns_table_name %>/show" do
14
14
 
15
15
  it "renders attributes in <p>" do
16
16
  render
17
- <% unless webrat? -%>
18
- # Run the generator again with the --webrat flag if you want to use webrat matchers
19
- <% end -%>
20
17
  <% for attribute in output_attributes -%>
21
- <% if webrat? -%>
22
- rendered.should contain(<%= value_for(attribute) %>.to_s)
23
- <% else -%>
24
- rendered.should match(/<%= eval(value_for(attribute)) %>/)
25
- <% end -%>
18
+ expect(rendered).to match(/<%= raw_value_for(attribute) %>/)
26
19
  <% end -%>
27
20
  end
28
21
  end
@@ -0,0 +1,24 @@
1
+ require 'generators/rspec'
2
+
3
+ module Rspec
4
+ module Generators
5
+ # @private
6
+ class SystemGenerator < Base
7
+ class_option :system_specs, type: :boolean, default: true, desc: "Generate system specs"
8
+
9
+ def generate_system_spec
10
+ return unless options[:system_specs]
11
+
12
+ template template_name, target_path('system', class_path, filename)
13
+ end
14
+
15
+ def template_name
16
+ 'system_spec.rb'
17
+ end
18
+
19
+ def filename
20
+ "#{table_name}_spec.rb"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:system) %> do
4
+ before do
5
+ driven_by(:rack_test)
6
+ end
7
+
8
+ pending "add some scenarios (or delete) #{__FILE__}"
9
+ end
@@ -1,5 +1,5 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
- describe "<%= file_path %>/<%= @action %>" do
3
+ RSpec.describe "<%= file_path %>/<%= @action %>", <%= type_metatag(:view) %> do
4
4
  pending "add some examples to (or delete) #{__FILE__}"
5
5
  end