rspec-rails 3.0.2 → 5.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) 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 +619 -43
  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 +22 -5
  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 +6 -0
  21. data/lib/generators/rspec/helper/helper_generator.rb +1 -1
  22. data/lib/generators/rspec/helper/templates/helper_spec.rb +1 -1
  23. data/lib/generators/rspec/install/install_generator.rb +22 -5
  24. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +43 -14
  25. data/lib/generators/rspec/integration/integration_generator.rb +7 -2
  26. data/lib/generators/rspec/integration/templates/request_spec.rb +3 -3
  27. data/lib/generators/rspec/job/job_generator.rb +13 -0
  28. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  29. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  30. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  31. data/lib/generators/rspec/mailer/mailer_generator.rb +8 -1
  32. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
  33. data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
  34. data/lib/generators/rspec/model/model_generator.rb +20 -6
  35. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  36. data/lib/generators/rspec/model/templates/model_spec.rb +1 -1
  37. data/lib/generators/rspec/request/request_generator.rb +10 -0
  38. data/lib/generators/rspec/scaffold/scaffold_generator.rb +90 -113
  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 +46 -64
  42. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +3 -3
  43. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -3
  44. data/lib/generators/rspec/scaffold/templates/new_spec.rb +3 -3
  45. data/lib/generators/rspec/scaffold/templates/request_spec.rb +138 -0
  46. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +18 -11
  47. data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
  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 +1 -1
  51. data/lib/generators/rspec/view/view_generator.rb +2 -2
  52. data/lib/generators/rspec.rb +12 -10
  53. data/lib/rspec/rails/active_record.rb +25 -0
  54. data/lib/rspec/rails/adapters.rb +34 -28
  55. data/lib/rspec/rails/configuration.rb +135 -40
  56. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  57. data/lib/rspec/rails/example/controller_example_group.rb +185 -149
  58. data/lib/rspec/rails/example/feature_example_group.rb +43 -23
  59. data/lib/rspec/rails/example/helper_example_group.rb +28 -25
  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 +27 -22
  63. data/lib/rspec/rails/example/model_example_group.rb +9 -6
  64. data/lib/rspec/rails/example/rails_example_group.rb +3 -2
  65. data/lib/rspec/rails/example/request_example_group.rb +21 -17
  66. data/lib/rspec/rails/example/routing_example_group.rb +49 -39
  67. data/lib/rspec/rails/example/system_example_group.rb +125 -0
  68. data/lib/rspec/rails/example/view_example_group.rb +178 -134
  69. data/lib/rspec/rails/example.rb +4 -0
  70. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -11
  71. data/lib/rspec/rails/feature_check.rb +47 -0
  72. data/lib/rspec/rails/file_fixture_support.rb +15 -0
  73. data/lib/rspec/rails/fixture_file_upload_support.rb +56 -0
  74. data/lib/rspec/rails/fixture_support.rb +42 -13
  75. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
  76. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  77. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  78. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  79. data/lib/rspec/rails/matchers/active_job.rb +465 -0
  80. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  81. data/lib/rspec/rails/matchers/be_a_new.rb +70 -64
  82. data/lib/rspec/rails/matchers/be_new_record.rb +25 -20
  83. data/lib/rspec/rails/matchers/be_valid.rb +39 -34
  84. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +226 -0
  85. data/lib/rspec/rails/matchers/have_http_status.rb +363 -333
  86. data/lib/rspec/rails/matchers/have_rendered.rb +55 -32
  87. data/lib/rspec/rails/matchers/redirect_to.rb +30 -27
  88. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  89. data/lib/rspec/rails/matchers/routing_matchers.rb +107 -101
  90. data/lib/rspec/rails/matchers.rb +20 -12
  91. data/lib/rspec/rails/tasks/rspec.rake +7 -17
  92. data/lib/rspec/rails/vendor/capybara.rb +12 -11
  93. data/lib/rspec/rails/version.rb +1 -1
  94. data/lib/rspec/rails/view_assigns.rb +1 -2
  95. data/lib/rspec/rails/view_path_builder.rb +29 -0
  96. data/lib/rspec/rails/view_rendering.rb +87 -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 +62 -3
  100. data.tar.gz.sig +0 -0
  101. metadata +118 -72
  102. metadata.gz.sig +0 -0
  103. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
@@ -17,9 +17,14 @@ require 'rails_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
- RSpec.describe <%= controller_class_name %>Controller, :type => :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
@@ -38,123 +43,100 @@ RSpec.describe <%= controller_class_name %>Controller, :type => :controller do
38
43
  let(:valid_session) { {} }
39
44
 
40
45
  <% unless options[:singleton] -%>
41
- describe "GET index" do
42
- it "assigns all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
43
- <%= file_name %> = <%= class_name %>.create! valid_attributes
44
- get :index, {}, valid_session
45
- expect(assigns(:<%= table_name %>)).to 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
46
51
  end
47
52
  end
48
53
 
49
54
  <% end -%>
50
- describe "GET show" do
51
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
55
+ describe "GET #show" do
56
+ it "returns a success response" do
52
57
  <%= file_name %> = <%= class_name %>.create! valid_attributes
53
- get :show, {:id => <%= file_name %>.to_param}, valid_session
54
- expect(assigns(:<%= ns_file_name %>)).to eq(<%= file_name %>)
58
+ get :show, params: {id: <%= file_name %>.to_param}, session: valid_session
59
+ expect(response).to be_successful
55
60
  end
56
61
  end
57
62
 
58
- describe "GET new" do
59
- it "assigns a new <%= ns_file_name %> as @<%= ns_file_name %>" do
60
- get :new, {}, valid_session
61
- expect(assigns(:<%= ns_file_name %>)).to 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
62
67
  end
63
68
  end
64
69
 
65
- describe "GET edit" do
66
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
70
+ describe "GET #edit" do
71
+ it "returns a success response" do
67
72
  <%= file_name %> = <%= class_name %>.create! valid_attributes
68
- get :edit, {:id => <%= file_name %>.to_param}, valid_session
69
- expect(assigns(:<%= ns_file_name %>)).to eq(<%= file_name %>)
73
+ get :edit, params: {id: <%= file_name %>.to_param}, session: valid_session
74
+ expect(response).to be_successful
70
75
  end
71
76
  end
72
77
 
73
- describe "POST create" do
74
- describe "with valid params" do
78
+ describe "POST #create" do
79
+ context "with valid params" do
75
80
  it "creates a new <%= class_name %>" do
76
81
  expect {
77
- post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
82
+ post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
78
83
  }.to change(<%= class_name %>, :count).by(1)
79
84
  end
80
85
 
81
- it "assigns a newly created <%= ns_file_name %> as @<%= ns_file_name %>" do
82
- post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
83
- expect(assigns(:<%= ns_file_name %>)).to be_a(<%= class_name %>)
84
- expect(assigns(:<%= ns_file_name %>)).to be_persisted
85
- end
86
-
87
- it "redirects to the created <%= ns_file_name %>" do
88
- post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
86
+ it "redirects to the created <%= singular_table_name %>" do
87
+ post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
89
88
  expect(response).to redirect_to(<%= class_name %>.last)
90
89
  end
91
90
  end
92
91
 
93
- describe "with invalid params" do
94
- it "assigns a newly created but unsaved <%= ns_file_name %> as @<%= ns_file_name %>" do
95
- post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session
96
- expect(assigns(:<%= ns_file_name %>)).to be_a_new(<%= class_name %>)
97
- end
98
-
99
- it "re-renders the 'new' template" do
100
- post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session
101
- expect(response).to render_template("new")
92
+ context "with invalid params" do
93
+ it "returns a success response (i.e. to display the 'new' template)" do
94
+ post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
95
+ expect(response).to be_successful
102
96
  end
103
97
  end
104
98
  end
105
99
 
106
- describe "PUT update" do
107
- describe "with valid params" do
100
+ describe "PUT #update" do
101
+ context "with valid params" do
108
102
  let(:new_attributes) {
109
103
  skip("Add a hash of attributes valid for your model")
110
104
  }
111
105
 
112
- it "updates the requested <%= ns_file_name %>" do
106
+ it "updates the requested <%= singular_table_name %>" do
113
107
  <%= file_name %> = <%= class_name %>.create! valid_attributes
114
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => new_attributes}, valid_session
108
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
115
109
  <%= file_name %>.reload
116
110
  skip("Add assertions for updated state")
117
111
  end
118
112
 
119
- it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
113
+ it "redirects to the <%= singular_table_name %>" do
120
114
  <%= file_name %> = <%= class_name %>.create! valid_attributes
121
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
122
- expect(assigns(:<%= ns_file_name %>)).to eq(<%= file_name %>)
123
- end
124
-
125
- it "redirects to the <%= ns_file_name %>" do
126
- <%= file_name %> = <%= class_name %>.create! valid_attributes
127
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
115
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
128
116
  expect(response).to redirect_to(<%= file_name %>)
129
117
  end
130
118
  end
131
119
 
132
- describe "with invalid params" do
133
- it "assigns the <%= ns_file_name %> as @<%= ns_file_name %>" do
134
- <%= file_name %> = <%= class_name %>.create! valid_attributes
135
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session
136
- expect(assigns(:<%= ns_file_name %>)).to eq(<%= file_name %>)
137
- end
138
-
139
- it "re-renders the 'edit' template" do
120
+ context "with invalid params" do
121
+ it "returns a success response (i.e. to display the 'edit' template)" do
140
122
  <%= file_name %> = <%= class_name %>.create! valid_attributes
141
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session
142
- expect(response).to render_template("edit")
123
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
124
+ expect(response).to be_successful
143
125
  end
144
126
  end
145
127
  end
146
128
 
147
- describe "DELETE destroy" do
148
- it "destroys the requested <%= ns_file_name %>" do
129
+ describe "DELETE #destroy" do
130
+ it "destroys the requested <%= singular_table_name %>" do
149
131
  <%= file_name %> = <%= class_name %>.create! valid_attributes
150
132
  expect {
151
- delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
133
+ delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
152
134
  }.to change(<%= class_name %>, :count).by(-1)
153
135
  end
154
136
 
155
137
  it "redirects to the <%= table_name %> list" do
156
138
  <%= file_name %> = <%= class_name %>.create! valid_attributes
157
- delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
139
+ delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
158
140
  expect(response).to redirect_to(<%= index_helper %>_url)
159
141
  end
160
142
  end
@@ -1,11 +1,11 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- RSpec.describe "<%= ns_table_name %>/edit", :type => :view do
4
+ RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
6
  @<%= ns_file_name %> = assign(:<%= ns_file_name %>, <%= 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
10
  <%= output_attributes.empty? ? "" : " ))\n" -%>
11
11
  end
@@ -16,7 +16,7 @@ RSpec.describe "<%= ns_table_name %>/edit", :type => :view do
16
16
  assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= ns_file_name %>), "post" do
17
17
  <% for attribute in output_attributes -%>
18
18
  <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
19
- assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]"
19
+ assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"
20
20
  <% end -%>
21
21
  end
22
22
  end
@@ -1,13 +1,13 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- RSpec.describe "<%= ns_table_name %>/index", :type => :view 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
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 ? ')' : '),' %>
@@ -19,7 +19,7 @@ RSpec.describe "<%= ns_table_name %>/index", :type => :view do
19
19
  it "renders a list of <%= ns_table_name %>" do
20
20
  render
21
21
  <% for attribute in output_attributes -%>
22
- assert_select "tr>td", :text => <%= value_for(attribute) %>.to_s, :count => 2
22
+ assert_select "tr>td", text: <%= value_for(attribute) %>.to_s, count: 2
23
23
  <% end -%>
24
24
  end
25
25
  end
@@ -1,11 +1,11 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- RSpec.describe "<%= ns_table_name %>/new", :type => :view do
4
+ RSpec.describe "<%= ns_table_name %>/new", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
6
  assign(:<%= ns_file_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
10
  <%= !output_attributes.empty? ? " ))\n end" : " end" %>
11
11
 
@@ -15,7 +15,7 @@ RSpec.describe "<%= ns_table_name %>/new", :type => :view do
15
15
  assert_select "form[action=?][method=?]", <%= index_helper %>_path, "post" do
16
16
  <% for attribute in output_attributes -%>
17
17
  <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
18
- assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]"
18
+ assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"
19
19
  <% end -%>
20
20
  end
21
21
  end
@@ -0,0 +1,138 @@
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
+ it "renders a successful response (i.e. to display the 'new' template)" do
87
+ post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
88
+ expect(response).to be_successful
89
+ end
90
+ end
91
+ end
92
+
93
+ describe "PATCH /update" do
94
+ context "with valid parameters" do
95
+ let(:new_attributes) {
96
+ skip("Add a hash of attributes valid for your model")
97
+ }
98
+
99
+ it "updates the requested <%= singular_table_name %>" do
100
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
101
+ patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
102
+ <%= file_name %>.reload
103
+ skip("Add assertions for updated state")
104
+ end
105
+
106
+ it "redirects to the <%= 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
+ expect(response).to redirect_to(<%= singular_table_name %>_url(<%= file_name %>))
111
+ end
112
+ end
113
+
114
+ context "with invalid parameters" do
115
+ it "renders a successful response (i.e. to display the 'edit' template)" do
116
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
117
+ patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
118
+ expect(response).to be_successful
119
+ end
120
+ end
121
+ end
122
+
123
+ describe "DELETE /destroy" do
124
+ it "destroys the requested <%= singular_table_name %>" do
125
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
126
+ expect {
127
+ delete <%= show_helper %>
128
+ }.to change(<%= class_name %>, :count).by(-1)
129
+ end
130
+
131
+ it "redirects to the <%= table_name %> list" do
132
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
133
+ delete <%= show_helper %>
134
+ expect(response).to redirect_to(<%= index_helper %>_url)
135
+ end
136
+ end
137
+ end
138
+ <% end -%>
@@ -1,39 +1,46 @@
1
1
  require "rails_helper"
2
2
 
3
3
  <% module_namespacing do -%>
4
- RSpec.describe <%= controller_class_name %>Controller, :type => :routing 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
- expect(:get => "/<%= ns_table_name %>").to 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
- expect(:get => "/<%= ns_table_name %>/new").to 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
- expect(:get => "/<%= ns_table_name %>/1").to 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
- expect(:get => "/<%= ns_table_name %>/1/edit").to 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
- expect(:post => "/<%= ns_table_name %>").to 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
- expect(:put => "/<%= ns_table_name %>/1").to 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
- expect(:delete => "/<%= ns_table_name %>/1").to 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
1
  require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- RSpec.describe "<%= ns_table_name %>/show", :type => :view do
4
+ RSpec.describe "<%= ns_table_name %>/show", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
6
  @<%= ns_file_name %> = assign(:<%= ns_file_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
  ))
@@ -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, File.join('spec/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
1
  require 'rails_helper'
2
2
 
3
- RSpec.describe "<%= file_path %>/<%= @action %>", :type => :view do
3
+ RSpec.describe "<%= file_path %>/<%= @action %>", <%= type_metatag(:view) %> do
4
4
  pending "add some examples to (or delete) #{__FILE__}"
5
5
  end
@@ -4,9 +4,9 @@ module Rspec
4
4
  module Generators
5
5
  # @private
6
6
  class ViewGenerator < Base
7
- argument :actions, :type => :array, :default => [], :banner => "action action"
7
+ argument :actions, type: :array, default: [], banner: "action action"
8
8
 
9
- class_option :template_engine, :desc => "Template engine to generate view files"
9
+ class_option :template_engine, desc: "Template engine to generate view files"
10
10
 
11
11
  def create_view_specs
12
12
  empty_directory File.join("spec", "views", file_path)
@@ -1,5 +1,7 @@
1
1
  require 'rails/generators/named_base'
2
+ require 'rspec/rails/feature_check'
2
3
 
4
+ # @private
3
5
  # Weirdly named generators namespace (should be `RSpec`) for compatability with
4
6
  # rails loading.
5
7
  module Rspec
@@ -7,13 +9,13 @@ module Rspec
7
9
  module Generators
8
10
  # @private
9
11
  class Base < ::Rails::Generators::NamedBase
10
- def self.source_root
11
- @_rspec_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'rspec', generator_name, 'templates'))
12
- end
12
+ include RSpec::Rails::FeatureCheck
13
13
 
14
- if ::Rails::VERSION::STRING < '3.1'
15
- def module_namespacing
16
- yield if block_given?
14
+ def self.source_root(path = nil)
15
+ if path
16
+ @_rspec_source_root = path
17
+ else
18
+ @_rspec_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'rspec', generator_name, 'templates'))
17
19
  end
18
20
  end
19
21
  end
@@ -27,10 +29,10 @@ module Rails
27
29
  class GeneratedAttribute
28
30
  def input_type
29
31
  @input_type ||= if type == :text
30
- "textarea"
31
- else
32
- "input"
33
- end
32
+ "textarea"
33
+ else
34
+ "input"
35
+ end
34
36
  end
35
37
  end
36
38
  end
@@ -0,0 +1,25 @@
1
+ module RSpec
2
+ module Rails
3
+ # Fake class to document RSpec ActiveRecord configuration options. In practice,
4
+ # these are dynamically added to the normal RSpec configuration object.
5
+ class ActiveRecordConfiguration
6
+ # @private
7
+ def self.initialize_activerecord_configuration(config)
8
+ config.before :suite do
9
+ # This allows dynamic columns etc to be used on ActiveRecord models when creating instance_doubles
10
+ if defined?(ActiveRecord) && defined?(ActiveRecord::Base) && defined?(::RSpec::Mocks) && (::RSpec::Mocks.respond_to?(:configuration))
11
+ ::RSpec::Mocks.configuration.when_declaring_verifying_double do |possible_model|
12
+ target = possible_model.target
13
+
14
+ if Class === target && ActiveRecord::Base > target && !target.abstract_class?
15
+ target.define_attribute_methods
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ initialize_activerecord_configuration RSpec.configuration
23
+ end
24
+ end
25
+ end