Skip to content

Commit

Permalink
Merge pull request #2694 from taketo1113/view-spec-with-model-name
Browse files Browse the repository at this point in the history
Fix variable name when generate scaffold with namespace & model-name
  • Loading branch information
JonRowe committed Oct 23, 2023
2 parents 9681266 + faa50ea commit 3f4b1e0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/generators/rspec/scaffold/templates/edit_spec.rb
Expand Up @@ -2,7 +2,7 @@

<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do
let(:<%= ns_file_name %>) {
let(:<%= singular_table_name %>) {
<%= class_name %>.create!(<%= ')' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
<%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
Expand All @@ -11,13 +11,13 @@
}
before(:each) do
assign(:<%= ns_file_name %>, <%= ns_file_name %>)
assign(:<%= singular_table_name %>, <%= singular_table_name %>)
end
it "renders the edit <%= ns_file_name %> form" do
render
assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(<%= ns_file_name %>), "post" do
assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(<%= singular_table_name %>), "post" do
<% for attribute in output_attributes -%>
<%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/scaffold/templates/new_spec.rb
Expand Up @@ -3,7 +3,7 @@
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
RSpec.describe "<%= ns_table_name %>/new", <%= type_metatag(:view) %> do
before(:each) do
assign(:<%= ns_file_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
assign(:<%= singular_table_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
<%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/scaffold/templates/show_spec.rb
Expand Up @@ -3,7 +3,7 @@
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
RSpec.describe "<%= ns_table_name %>/show", <%= type_metatag(:view) %> do
before(:each) do
assign(:<%= ns_file_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
assign(:<%= singular_table_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
<%= attribute.name %>: <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
Expand Down
60 changes: 60 additions & 0 deletions spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Expand Up @@ -192,6 +192,7 @@
it { is_expected.to exist }
it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe "(.*)\/edit", #{type_metatag(:view)}/) }
it { is_expected.to contain(/assign\(:post, post\)/) }
it { is_expected.to contain(/it "renders the edit (.*) form"/) }
end

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

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

Expand All @@ -216,6 +219,7 @@
it { is_expected.to exist }
it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe "(.*)\/show", #{type_metatag(:view)}/) }
it { is_expected.to contain(/assign\(:post, /) }
it { is_expected.to contain(/it "renders attributes in <p>"/) }
end
end
Expand Down Expand Up @@ -251,6 +255,62 @@
end
end

describe 'with namespace' do
before { run_generator %w[admin/posts] }

describe 'edit' do
subject { file("spec/views/admin/posts/edit.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:admin_post, admin_post\)/) }
end

describe 'index' do
subject { file("spec/views/admin/posts/index.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:admin_posts, /) }
end

describe 'new' do
subject { file("spec/views/admin/posts/new.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:admin_post, /) }
end

describe 'show' do
subject { file("spec/views/admin/posts/show.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:admin_post, /) }
end
end

describe 'with namespace and --model-name' do
before { run_generator %w[admin/posts --model-name=Post] }

describe 'edit' do
subject { file("spec/views/admin/posts/edit.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:post, post\)/) }
end

describe 'index' do
subject { file("spec/views/admin/posts/index.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:posts, /) }
end

describe 'new' do
subject { file("spec/views/admin/posts/new.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:post, /) }
end

describe 'show' do
subject { file("spec/views/admin/posts/show.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:post, /) }
end
end

describe 'with --no-template-engine' do
before { run_generator %w[posts --no-template-engine] }
describe 'edit' do
Expand Down

0 comments on commit 3f4b1e0

Please sign in to comment.