Skip to content

Commit 3bb2588

Browse files
committed
Merge pull request #2694 from taketo1113/view-spec-with-model-name
Fix variable name when generate scaffold with namespace & model-name
1 parent ae4b163 commit 3bb2588

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

lib/generators/rspec/scaffold/templates/edit_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
44
RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do
5-
let(:<%= ns_file_name %>) {
5+
let(:<%= singular_table_name %>) {
66
<%= class_name %>.create!(<%= ')' if output_attributes.empty? %>
77
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
88
<%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
@@ -11,13 +11,13 @@
1111
}
1212
1313
before(:each) do
14-
assign(:<%= ns_file_name %>, <%= ns_file_name %>)
14+
assign(:<%= singular_table_name %>, <%= singular_table_name %>)
1515
end
1616
1717
it "renders the edit <%= ns_file_name %> form" do
1818
render
1919
20-
assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(<%= ns_file_name %>), "post" do
20+
assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(<%= singular_table_name %>), "post" do
2121
<% for attribute in output_attributes -%>
2222
<%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
2323
assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"

lib/generators/rspec/scaffold/templates/new_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
44
RSpec.describe "<%= ns_table_name %>/new", <%= type_metatag(:view) %> do
55
before(:each) do
6-
assign(:<%= ns_file_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
6+
assign(:<%= singular_table_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
77
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
88
<%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
99
<% end -%>

lib/generators/rspec/scaffold/templates/show_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
44
RSpec.describe "<%= ns_table_name %>/show", <%= type_metatag(:view) %> do
55
before(:each) do
6-
assign(:<%= ns_file_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
6+
assign(:<%= singular_table_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
77
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
88
<%= attribute.name %>: <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
99
<% end -%>

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
it { is_expected.to exist }
193193
it { is_expected.to contain(/require 'rails_helper'/) }
194194
it { is_expected.to contain(/^RSpec.describe "(.*)\/edit", #{type_metatag(:view)}/) }
195+
it { is_expected.to contain(/assign\(:post, post\)/) }
195196
it { is_expected.to contain(/it "renders the edit (.*) form"/) }
196197
end
197198

@@ -200,6 +201,7 @@
200201
it { is_expected.to exist }
201202
it { is_expected.to contain(/require 'rails_helper'/) }
202203
it { is_expected.to contain(/^RSpec.describe "(.*)\/index", #{type_metatag(:view)}/) }
204+
it { is_expected.to contain(/assign\(:posts, /) }
203205
it { is_expected.to contain(/it "renders a list of (.*)"/) }
204206
end
205207

@@ -208,6 +210,7 @@
208210
it { is_expected.to exist }
209211
it { is_expected.to contain(/require 'rails_helper'/) }
210212
it { is_expected.to contain(/^RSpec.describe "(.*)\/new", #{type_metatag(:view)}/) }
213+
it { is_expected.to contain(/assign\(:post, /) }
211214
it { is_expected.to contain(/it "renders new (.*) form"/) }
212215
end
213216

@@ -216,6 +219,7 @@
216219
it { is_expected.to exist }
217220
it { is_expected.to contain(/require 'rails_helper'/) }
218221
it { is_expected.to contain(/^RSpec.describe "(.*)\/show", #{type_metatag(:view)}/) }
222+
it { is_expected.to contain(/assign\(:post, /) }
219223
it { is_expected.to contain(/it "renders attributes in <p>"/) }
220224
end
221225
end
@@ -251,6 +255,62 @@
251255
end
252256
end
253257

258+
describe 'with namespace' do
259+
before { run_generator %w[admin/posts] }
260+
261+
describe 'edit' do
262+
subject { file("spec/views/admin/posts/edit.html.erb_spec.rb") }
263+
it { is_expected.to exist }
264+
it { is_expected.to contain(/assign\(:admin_post, admin_post\)/) }
265+
end
266+
267+
describe 'index' do
268+
subject { file("spec/views/admin/posts/index.html.erb_spec.rb") }
269+
it { is_expected.to exist }
270+
it { is_expected.to contain(/assign\(:admin_posts, /) }
271+
end
272+
273+
describe 'new' do
274+
subject { file("spec/views/admin/posts/new.html.erb_spec.rb") }
275+
it { is_expected.to exist }
276+
it { is_expected.to contain(/assign\(:admin_post, /) }
277+
end
278+
279+
describe 'show' do
280+
subject { file("spec/views/admin/posts/show.html.erb_spec.rb") }
281+
it { is_expected.to exist }
282+
it { is_expected.to contain(/assign\(:admin_post, /) }
283+
end
284+
end
285+
286+
describe 'with namespace and --model-name' do
287+
before { run_generator %w[admin/posts --model-name=Post] }
288+
289+
describe 'edit' do
290+
subject { file("spec/views/admin/posts/edit.html.erb_spec.rb") }
291+
it { is_expected.to exist }
292+
it { is_expected.to contain(/assign\(:post, post\)/) }
293+
end
294+
295+
describe 'index' do
296+
subject { file("spec/views/admin/posts/index.html.erb_spec.rb") }
297+
it { is_expected.to exist }
298+
it { is_expected.to contain(/assign\(:posts, /) }
299+
end
300+
301+
describe 'new' do
302+
subject { file("spec/views/admin/posts/new.html.erb_spec.rb") }
303+
it { is_expected.to exist }
304+
it { is_expected.to contain(/assign\(:post, /) }
305+
end
306+
307+
describe 'show' do
308+
subject { file("spec/views/admin/posts/show.html.erb_spec.rb") }
309+
it { is_expected.to exist }
310+
it { is_expected.to contain(/assign\(:post, /) }
311+
end
312+
end
313+
254314
describe 'with --no-template-engine' do
255315
before { run_generator %w[posts --no-template-engine] }
256316
describe 'edit' do

0 commit comments

Comments
 (0)