Commit aff5ef79 by liyijie

fix spec generator use resource model name

parent 31a98a9e
...@@ -141,6 +141,14 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -141,6 +141,14 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
options.resource&.singularize || resource_class&.model_name&.element&.singularize options.resource&.singularize || resource_class&.model_name&.element&.singularize
end end
def resource_model_plural
resource_class&.model_name&.plural
end
def resource_model_singular
resource_class&.model_name&.singular
end
def resource_collection def resource_collection
resource_class&.model_name&.collection resource_class&.model_name&.collection
end end
......
...@@ -15,15 +15,15 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, ...@@ -15,15 +15,15 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
} }
} }
} }
<%= resource_singular %>_value = FactoryBot.attributes_for(:<%= resource_singular %>) <%= resource_singular %>_value = FactoryBot.attributes_for(:<%= resource_model_singular %>)
before :each do before :each do
<% if auth.present? -%> <% if auth.present? -%>
@<%= auth_singular %> = <%= auth %>.register "<%= auth_singular %>", "password" @<%= auth_singular %> = <%= auth %>.register "<%= auth_singular %>", "password"
@<%= auth_singular %>_token = <%= auth %>.login "<%= auth_singular %>", "password" @<%= auth_singular %>_token = <%= auth %>.login "<%= auth_singular %>", "password"
<% end -%> <% end -%>
@<%= resource_singular %>_count = 5 @<%= resource_model_singular %>_count = 5
@<%= resource_plural %> = FactoryBot.create_list(:<%= resource_singular %>, @<%= resource_singular %>_count) @<%= resource_model_plural %> = FactoryBot.create_list(:<%= resource_model_singular %>, @<%= resource_model_singular %>_count)
end end
<% @routes.each do | template, path_item | -%> <% @routes.each do | template, path_item | -%>
...@@ -50,7 +50,7 @@ resource_singular %>_ref ...@@ -50,7 +50,7 @@ resource_singular %>_ref
response(<%= response_status action %>, description: 'successful') do response(<%= response_status action %>, description: 'successful') do
<% path_item[:params].each do |param| -%> <% path_item[:params].each do |param| -%>
<% if param == 'id' -%> <% if param == 'id' -%>
let(:<%= param %>) { @<%= resource_plural %>.first.id } let(:<%= param %>) { @<%= resource_model_plural %>.first.id }
<% elsif param.end_with? '_id' -%> <% elsif param.end_with? '_id' -%>
<% model_name = param.sub('_id', '') -%> <% model_name = param.sub('_id', '') -%>
let(:<%= param %>) { @<%= model_name %>.id } let(:<%= param %>) { @<%= model_name %>.id }
...@@ -71,16 +71,16 @@ resource_singular %>_ref ...@@ -71,16 +71,16 @@ resource_singular %>_ref
<% elsif details[:summary].start_with?("list") -%> <% elsif details[:summary].start_with?("list") -%>
it { it {
body = JSON.parse(response.body) body = JSON.parse(response.body)
expect(body['<%= resource_plural %>'].count).to eq @<%= resource_singular %>_count expect(body['<%= resource_plural %>'].count).to eq @<%= resource_model_singular %>_count
} }
<% elsif details[:summary].start_with?("delete") -%> <% elsif details[:summary].start_with?("delete") -%>
it { it {
expect(<%= resource_class %>.count).to eq(@<%= resource_singular %>_count-1) expect(<%= resource_class %>.count).to eq(@<%= resource_model_singular %>_count-1)
} }
<% elsif details[:summary].start_with?("show") -%> <% elsif details[:summary].start_with?("show") -%>
it { it {
body = JSON.parse(response.body) body = JSON.parse(response.body)
<%= resource_singular %> = @<%= resource_plural %>.first <%= resource_singular %> = @<%= resource_model_plural %>.first
<% attributes_names.each do |attr| -%> <% attributes_names.each do |attr| -%>
expect(body['<%= attr %>']).to eq <%= resource_singular %>.<%= attr %> expect(body['<%= attr %>']).to eq <%= resource_singular %>.<%= attr %>
<% end -%> <% end -%>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment