diff --git a/lib/templates/rspec/channel/channel_spec.rb.erb b/lib/templates/rspec/channel/channel_spec.rb.erb new file mode 100644 index 00000000..aa067669 --- /dev/null +++ b/lib/templates/rspec/channel/channel_spec.rb.erb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% module_namespacing do -%> +RSpec.describe <%= class_name %>Channel, <%= type_metatag(:channel) %> do + pending "add some examples to (or delete) #{__FILE__}" +end +<% end -%> diff --git a/lib/templates/rspec/controller/controller_spec.rb b/lib/templates/rspec/controller/controller_spec.rb new file mode 100644 index 00000000..e691da01 --- /dev/null +++ b/lib/templates/rspec/controller/controller_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% module_namespacing do -%> +RSpec.describe <%= class_name %>Controller, <%= type_metatag(:controller) %> do + +<% for action in actions -%> + describe "GET #<%= action %>" do + it "returns http success" do + get :<%= action %> + expect(response).to have_http_status(:success) + end + end + +<% end -%> +end +<% end -%> diff --git a/lib/templates/rspec/controller/request_spec.rb b/lib/templates/rspec/controller/request_spec.rb new file mode 100644 index 00000000..3270392f --- /dev/null +++ b/lib/templates/rspec/controller/request_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do +<% namespaced_path = regular_class_path.join('/') %> +<% for action in actions -%> + describe "GET /<%= action %>" do + it "returns http success" do + get "<%= "/#{namespaced_path}" if namespaced_path != '' %>/<%= file_name %>/<%= action %>" + expect(response).to have_http_status(:success) + end + end + +<% end -%> +end diff --git a/lib/templates/rspec/controller/routing_spec.rb b/lib/templates/rspec/controller/routing_spec.rb new file mode 100644 index 00000000..9f211e39 --- /dev/null +++ b/lib/templates/rspec/controller/routing_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% module_namespacing do -%> +RSpec.describe '<%= class_name %>Controller', <%= type_metatag(:routing) %> do + describe 'routing' do +<% for action in actions -%> + it 'routes to #<%= action %>' do + expect(get: "/<%= class_name.underscore %>/<%= action %>").to route_to("<%= class_name.underscore %>#<%= action %>") + end +<% end -%> + end +end +<% end -%> diff --git a/lib/templates/rspec/controller/view_spec.rb b/lib/templates/rspec/controller/view_spec.rb new file mode 100644 index 00000000..531bc44b --- /dev/null +++ b/lib/templates/rspec/controller/view_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +RSpec.describe "<%= file_name %>/<%= @action %>.html.<%= options[:template_engine] %>", <%= type_metatag(:view) %> do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/lib/templates/rspec/feature/feature_singular_spec.rb b/lib/templates/rspec/feature/feature_singular_spec.rb new file mode 100644 index 00000000..67968f78 --- /dev/null +++ b/lib/templates/rspec/feature/feature_singular_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +RSpec.feature "<%= class_name.singularize %>", <%= type_metatag(:feature) %> do + pending "add some scenarios (or delete) #{__FILE__}" +end diff --git a/lib/templates/rspec/feature/feature_spec.rb b/lib/templates/rspec/feature/feature_spec.rb new file mode 100644 index 00000000..22b6daef --- /dev/null +++ b/lib/templates/rspec/feature/feature_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +RSpec.feature "<%= class_name.pluralize %>", <%= type_metatag(:feature) %> do + pending "add some scenarios (or delete) #{__FILE__}" +end diff --git a/lib/templates/rspec/generator/generator_spec.rb b/lib/templates/rspec/generator/generator_spec.rb new file mode 100644 index 00000000..1d4a4791 --- /dev/null +++ b/lib/templates/rspec/generator/generator_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:generator) %> do + + pending "add some scenarios (or delete) #{__FILE__}" +end diff --git a/lib/templates/rspec/helper/helper_spec.rb b/lib/templates/rspec/helper/helper_spec.rb new file mode 100644 index 00000000..7c5e51c2 --- /dev/null +++ b/lib/templates/rspec/helper/helper_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the <%= class_name %>Helper. For example: +# +# describe <%= class_name %>Helper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +<% module_namespacing do -%> +RSpec.describe <%= class_name %>Helper, <%= type_metatag(:helper) %> do + pending "add some examples to (or delete) #{__FILE__}" +end +<% end -%> diff --git a/lib/templates/rspec/install/spec/rails_helper.rb b/lib/templates/rspec/install/spec/rails_helper.rb new file mode 100644 index 00000000..4a86c981 --- /dev/null +++ b/lib/templates/rspec/install/spec/rails_helper.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../config/environment', __dir__) +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +<% if RSpec::Rails::FeatureCheck.has_active_record_migration? -%> +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + puts e.to_s.strip + exit 1 +end +<% end -%> +RSpec.configure do |config| +<% if RSpec::Rails::FeatureCheck.has_active_record? -%> + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + +<% else -%> + # Remove this line to enable support for ActiveRecord + config.use_active_record = false + + # If you enable ActiveRecord support you should unncomment these lines, + # note if you'd prefer not to run each example within a transaction, you + # should set use_transactional_fixtures to false. + # + # config.fixture_path = "#{::Rails.root}/spec/fixtures" + # config.use_transactional_fixtures = true + +<% end -%> + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/lib/templates/rspec/integration/request_spec.rb b/lib/templates/rspec/integration/request_spec.rb new file mode 100644 index 00000000..2106d5a1 --- /dev/null +++ b/lib/templates/rspec/integration/request_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do + describe "GET /<%= name.underscore.pluralize %>" do + it "works! (now write some real specs)" do + get <%= index_helper %>_path + expect(response).to have_http_status(200) + end + end +end diff --git a/lib/templates/rspec/job/job_spec.rb.erb b/lib/templates/rspec/job/job_spec.rb.erb new file mode 100644 index 00000000..dbcb7e55 --- /dev/null +++ b/lib/templates/rspec/job/job_spec.rb.erb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% module_namespacing do -%> +RSpec.describe <%= class_name %>Job, <%= type_metatag(:job) %> do + pending "add some examples to (or delete) #{__FILE__}" +end +<% end -%> diff --git a/lib/templates/rspec/mailbox/mailbox_spec.rb.erb b/lib/templates/rspec/mailbox/mailbox_spec.rb.erb new file mode 100644 index 00000000..7280025c --- /dev/null +++ b/lib/templates/rspec/mailbox/mailbox_spec.rb.erb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% module_namespacing do -%> +RSpec.describe <%= class_name %>Mailbox, <%= type_metatag(:mailbox) %> do + pending "add some examples to (or delete) #{__FILE__}" +end +<% end -%> diff --git a/lib/templates/rspec/mailer/fixture b/lib/templates/rspec/mailer/fixture new file mode 100644 index 00000000..171648d6 --- /dev/null +++ b/lib/templates/rspec/mailer/fixture @@ -0,0 +1,3 @@ +<%= class_name %>#<%= @action %> + +Hi, find me in app/views/<%= @path %> diff --git a/lib/templates/rspec/mailer/mailer_spec.rb b/lib/templates/rspec/mailer/mailer_spec.rb new file mode 100644 index 00000000..c72a60ec --- /dev/null +++ b/lib/templates/rspec/mailer/mailer_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require "rails_helper" + +<% module_namespacing do -%> +RSpec.describe <%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>, <%= type_metatag(:mailer) %> do +<% for action in actions -%> + describe "<%= action %>" do + let(:mail) { <%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>.<%= action %> } + + it "renders the headers" do + expect(mail.subject).to eq(<%= action.to_s.humanize.inspect %>) + expect(mail.to).to eq(["to@example.org"]) + expect(mail.from).to eq(["from@example.com"]) + end + + it "renders the body" do + expect(mail.body.encoded).to match("Hi") + end + end + +<% end -%> +<% if actions.blank? -%> + pending "add some examples to (or delete) #{__FILE__}" +<% end -%> +end +<% end -%> diff --git a/lib/templates/rspec/mailer/preview.rb b/lib/templates/rspec/mailer/preview.rb new file mode 100644 index 00000000..d8bfa892 --- /dev/null +++ b/lib/templates/rspec/mailer/preview.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +<% module_namespacing do -%> +# Preview all emails at http://localhost:3000/rails/mailers/<%= file_path %> +class <%= class_name %>Preview < ActionMailer::Preview +<% actions.each do |action| -%> + + # Preview this email at http://localhost:3000/rails/mailers/<%= file_path %>/<%= action %> + def <%= action %> + <%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>.<%= action %> + end +<% end -%> + +end +<% end -%> diff --git a/lib/templates/rspec/model/fixtures.yml b/lib/templates/rspec/model/fixtures.yml new file mode 100644 index 00000000..4a8ab4b4 --- /dev/null +++ b/lib/templates/rspec/model/fixtures.yml @@ -0,0 +1,19 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +<% unless attributes.empty? -%> +one: +<% for attribute in attributes -%> + <%= attribute.name %>: <%= attribute.default %> +<% end -%> + +two: +<% for attribute in attributes -%> + <%= attribute.name %>: <%= attribute.default %> +<% end -%> +<% else -%> +# one: +# column: value +# +# two: +# column: value +<% end -%> diff --git a/lib/templates/rspec/model/model_spec.rb b/lib/templates/rspec/model/model_spec.rb new file mode 100644 index 00000000..5a4acc49 --- /dev/null +++ b/lib/templates/rspec/model/model_spec.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% module_namespacing do -%> +RSpec.describe <%= class_name %>, <%= type_metatag(:model) %> do + pending "add some examples to (or delete) #{__FILE__}" +end +<% end -%> diff --git a/lib/templates/rspec/scaffold/api_controller_spec.rb b/lib/templates/rspec/scaffold/api_controller_spec.rb new file mode 100644 index 00000000..62b5b0b3 --- /dev/null +++ b/lib/templates/rspec/scaffold/api_controller_spec.rb @@ -0,0 +1,132 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. +# +# Also compared to earlier versions of this generator, there are no longer any +# expectations of assigns and templates rendered. These features have been +# removed from Rails core in Rails 5, but can be added back in via the +# `rails-controller-testing` gem. + +<% module_namespacing do -%> +RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:controller) %> do + + # This should return the minimal set of attributes required to create a valid + # <%= class_name %>. As you add validations to <%= class_name %>, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # <%= controller_class_name %>Controller. Be sure to keep this updated too. + let(:valid_session) { {} } + +<% unless options[:singleton] -%> + describe "GET #index" do + it "returns a success response" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + get :index, params: {}, session: valid_session + expect(response).to be_successful + end + end + +<% end -%> + describe "GET #show" do + it "returns a success response" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + get :show, params: {id: <%= file_name %>.to_param}, session: valid_session + expect(response).to be_successful + end + end + + describe "POST #create" do + context "with valid params" do + it "creates a new <%= class_name %>" do + expect { + post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session + }.to change(<%= class_name %>, :count).by(1) + end + + it "renders a JSON response with the new <%= ns_file_name %>" do + post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session + expect(response).to have_http_status(:created) + expect(response.content_type).to eq('application/json') + expect(response.location).to eq(<%= ns_file_name %>_url(<%= class_name %>.last)) + end + end + + context "with invalid params" do + it "renders a JSON response with errors for the new <%= ns_file_name %>" do + post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session + expect(response).to have_http_status(:unprocessable_entity) + expect(response.content_type).to eq('application/json') + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session + <%= file_name %>.reload + skip("Add assertions for updated state") + end + + it "renders a JSON response with the <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session + expect(response).to have_http_status(:ok) + expect(response.content_type).to eq('application/json') + end + end + + context "with invalid params" do + it "renders a JSON response with errors for the <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session + expect(response).to have_http_status(:unprocessable_entity) + expect(response.content_type).to eq('application/json') + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + expect { + delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session + }.to change(<%= class_name %>, :count).by(-1) + end + end + +end +<% end -%> diff --git a/lib/templates/rspec/scaffold/api_request_spec.rb b/lib/templates/rspec/scaffold/api_request_spec.rb new file mode 100644 index 00000000..0fec9c9b --- /dev/null +++ b/lib/templates/rspec/scaffold/api_request_spec.rb @@ -0,0 +1,134 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to test the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. + +<% module_namespacing do -%> +RSpec.describe "/<%= name.underscore.pluralize %>", <%= type_metatag(:request) %> do + # This should return the minimal set of attributes required to create a valid + # <%= class_name %>. As you add validations to <%= class_name %>, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + # This should return the minimal set of values that should be in the headers + # in order to pass any filters (e.g. authentication) defined in + # <%= controller_class_name %>Controller, or in your router and rack + # middleware. Be sure to keep this updated too. + let(:valid_headers) { + {} + } + +<% unless options[:singleton] -%> + describe "GET /index" do + it "renders a successful response" do + <%= class_name %>.create! valid_attributes + get <%= index_helper %>_url, headers: valid_headers, as: :json + expect(response).to be_successful + end + end +<% end -%> + + describe "GET /show" do + it "renders a successful response" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + get <%= show_helper.tr('@', '') %>, as: :json + expect(response).to be_successful + end + end + + describe "POST /create" do + context "with valid parameters" do + it "creates a new <%= class_name %>" do + expect { + post <%= index_helper %>_url, + params: { <%= ns_file_name %>: valid_attributes }, headers: valid_headers, as: :json + }.to change(<%= class_name %>, :count).by(1) + end + + it "renders a JSON response with the new <%= ns_file_name %>" do + post <%= index_helper %>_url, + params: { <%= ns_file_name %>: valid_attributes }, headers: valid_headers, as: :json + expect(response).to have_http_status(:created) + expect(response.content_type).to match(a_string_including("application/json")) + end + end + + context "with invalid parameters" do + it "does not create a new <%= class_name %>" do + expect { + post <%= index_helper %>_url, + params: { <%= ns_file_name %>: invalid_attributes }, as: :json + }.to change(<%= class_name %>, :count).by(0) + end + + it "renders a JSON response with errors for the new <%= ns_file_name %>" do + post <%= index_helper %>_url, + params: { <%= ns_file_name %>: invalid_attributes }, headers: valid_headers, as: :json + expect(response).to have_http_status(:unprocessable_entity) + expect(response.content_type).to eq("application/json") + end + end + end + + describe "PATCH /update" do + context "with valid parameters" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + patch <%= show_helper.tr('@', '') %>, + params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json + <%= file_name %>.reload + skip("Add assertions for updated state") + end + + it "renders a JSON response with the <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + patch <%= show_helper.tr('@', '') %>, + params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json + expect(response).to have_http_status(:ok) + expect(response.content_type).to eq("application/json") + end + end + + context "with invalid parameters" do + it "renders a JSON response with errors for the <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + patch <%= show_helper.tr('@', '') %>, + params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json + expect(response).to have_http_status(:unprocessable_entity) + expect(response.content_type).to eq("application/json") + end + end + end + + describe "DELETE /destroy" do + it "destroys the requested <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + expect { + delete <%= show_helper.tr('@', '') %>, headers: valid_headers, as: :json + }.to change(<%= class_name %>, :count).by(-1) + end + end +end +<% end -%> diff --git a/lib/templates/rspec/scaffold/controller_spec.rb b/lib/templates/rspec/scaffold/controller_spec.rb new file mode 100644 index 00000000..f6d16a9b --- /dev/null +++ b/lib/templates/rspec/scaffold/controller_spec.rb @@ -0,0 +1,196 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. +# +# Also compared to earlier versions of this generator, there are no longer any +# expectations of assigns and templates rendered. These features have been +# removed from Rails core in Rails 5, but can be added back in via the +# `rails-controller-testing` gem. + +<% module_namespacing do -%> +RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:controller) %> do + + # This should return the minimal set of attributes required to create a valid + # <%= class_name %>. As you add validations to <%= class_name %>, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # <%= controller_class_name %>Controller. Be sure to keep this updated too. + let(:valid_session) { {} } + +<% unless options[:singleton] -%> + describe "GET #index" do + it "returns a success response" do + <%= class_name %>.create! valid_attributes +<% if Rails::VERSION::STRING < '5.0' -%> + get :index, {}, valid_session +<% else -%> + get :index, params: {}, session: valid_session +<% end -%> + expect(response).to be_successful + end + end + +<% end -%> + describe "GET #show" do + it "returns a success response" do + <%= file_name %> = <%= class_name %>.create! valid_attributes +<% if Rails::VERSION::STRING < '5.0' -%> + get :show, {id: <%= file_name %>.to_param}, valid_session +<% else -%> + get :show, params: {id: <%= file_name %>.to_param}, session: valid_session +<% end -%> + expect(response).to be_successful + end + end + + describe "GET #new" do + it "returns a success response" do +<% if Rails::VERSION::STRING < '5.0' -%> + get :new, {}, valid_session +<% else -%> + get :new, params: {}, session: valid_session +<% end -%> + expect(response).to be_successful + end + end + + describe "GET #edit" do + it "returns a success response" do + <%= file_name %> = <%= class_name %>.create! valid_attributes +<% if Rails::VERSION::STRING < '5.0' -%> + get :edit, {id: <%= file_name %>.to_param}, valid_session +<% else -%> + get :edit, params: {id: <%= file_name %>.to_param}, session: valid_session +<% end -%> + expect(response).to be_successful + end + end + + describe "POST #create" do + context "with valid params" do + it "creates a new <%= class_name %>" do + expect { +<% if Rails::VERSION::STRING < '5.0' -%> + post :create, {<%= ns_file_name %>: valid_attributes}, valid_session +<% else -%> + post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session +<% end -%> + }.to change(<%= class_name %>, :count).by(1) + end + + it "redirects to the created <%= ns_file_name %>" do +<% if Rails::VERSION::STRING < '5.0' -%> + post :create, {<%= ns_file_name %>: valid_attributes}, valid_session +<% else -%> + post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session +<% end -%> + expect(response).to redirect_to(<%= class_name %>.last) + end + end + + context "with invalid params" do + it "returns a success response (i.e. to display the 'new' template)" do +<% if Rails::VERSION::STRING < '5.0' -%> + post :create, {<%= ns_file_name %>: invalid_attributes}, valid_session +<% else -%> + post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session +<% end -%> + expect(response).to be_successful + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes +<% if Rails::VERSION::STRING < '5.0' -%> + put :update, {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, valid_session +<% else -%> + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session +<% end -%> + <%= file_name %>.reload + skip("Add assertions for updated state") + end + + it "redirects to the <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes +<% if Rails::VERSION::STRING < '5.0' -%> + put :update, {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, valid_session +<% else -%> + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session +<% end -%> + expect(response).to redirect_to(<%= file_name %>) + end + end + + context "with invalid params" do + it "returns a success response (i.e. to display the 'edit' template)" do + <%= file_name %> = <%= class_name %>.create! valid_attributes +<% if Rails::VERSION::STRING < '5.0' -%> + put :update, {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, valid_session +<% else -%> + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session +<% end -%> + expect(response).to be_successful + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + expect { +<% if Rails::VERSION::STRING < '5.0' -%> + delete :destroy, {id: <%= file_name %>.to_param}, valid_session +<% else -%> + delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session +<% end -%> + }.to change(<%= class_name %>, :count).by(-1) + end + + it "redirects to the <%= table_name %> list" do + <%= file_name %> = <%= class_name %>.create! valid_attributes +<% if Rails::VERSION::STRING < '5.0' -%> + delete :destroy, {id: <%= file_name %>.to_param}, valid_session +<% else -%> + delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session +<% end -%> + expect(response).to redirect_to(<%= index_helper %>_url) + end + end + +end +<% end -%> diff --git a/lib/templates/rspec/scaffold/edit_spec.rb b/lib/templates/rspec/scaffold/edit_spec.rb new file mode 100644 index 00000000..a7ea64ed --- /dev/null +++ b/lib/templates/rspec/scaffold/edit_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%> +RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do + before(:each) do + @<%= ns_file_name %> = assign(:<%= ns_file_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 ? '' : ','%> +<% end -%> +<%= output_attributes.empty? ? "" : " ))\n" -%> + 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 +<% for attribute in output_attributes -%> + <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %> +<% if Rails.version.to_f >= 5.1 -%> + assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]" +<% else -%> + assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]" +<% end -%> +<% end -%> + end + end +end diff --git a/lib/templates/rspec/scaffold/index_spec.rb b/lib/templates/rspec/scaffold/index_spec.rb new file mode 100644 index 00000000..cb703f1b --- /dev/null +++ b/lib/templates/rspec/scaffold/index_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%> +RSpec.describe "<%= ns_table_name %>/index", <%= type_metatag(:view) %> do + before(:each) do + assign(:<%= table_name %>, [ +<% [1,2].each_with_index do |id, model_index| -%> + <%= class_name %>.create!(<%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : '' %> +<% output_attributes.each_with_index do |attribute, attribute_index| -%> + <%= attribute.name %>: <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%> +<% end -%> +<% if !output_attributes.empty? -%> + <%= model_index == 1 ? ')' : '),' %> +<% end -%> +<% end -%> + ]) + end + + it "renders a list of <%= ns_table_name %>" do + render +<% for attribute in output_attributes -%> + assert_select "tr>td", text: <%= value_for(attribute) %>.to_s, count: 2 +<% end -%> + end +end diff --git a/lib/templates/rspec/scaffold/new_spec.rb b/lib/templates/rspec/scaffold/new_spec.rb new file mode 100644 index 00000000..befa8d4d --- /dev/null +++ b/lib/templates/rspec/scaffold/new_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% 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? %> +<% output_attributes.each_with_index do |attribute, attribute_index| -%> + <%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%> +<% end -%> +<%= !output_attributes.empty? ? " ))\n end" : " end" %> + + it "renders new <%= ns_file_name %> form" do + render + + assert_select "form[action=?][method=?]", <%= index_helper %>_path, "post" do +<% for attribute in output_attributes -%> + <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %> +<% if Rails.version.to_f >= 5.1 -%> + assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]" +<% else -%> + assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]" +<% end -%> +<% end -%> + end + end +end diff --git a/lib/templates/rspec/scaffold/request_spec.rb b/lib/templates/rspec/scaffold/request_spec.rb new file mode 100644 index 00000000..b575879f --- /dev/null +++ b/lib/templates/rspec/scaffold/request_spec.rb @@ -0,0 +1,136 @@ + # frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later + require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to test the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. + +<% module_namespacing do -%> +RSpec.describe "/<%= name.underscore.pluralize %>", <%= type_metatag(:request) %> do + # <%= class_name %>. As you add validations to <%= class_name %>, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + +<% unless options[:singleton] -%> + describe "GET /index" do + it "renders a successful response" do + <%= class_name %>.create! valid_attributes + get <%= index_helper %>_url + expect(response).to be_successful + end + end +<% end -%> + + describe "GET /show" do + it "renders a successful response" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + get <%= show_helper.tr('@', '') %> + expect(response).to be_successful + end + end + + describe "GET /new" do + it "renders a successful response" do + get <%= new_helper %> + expect(response).to be_successful + end + end + + describe "GET /edit" do + it "render a successful response" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + get <%= edit_helper.tr('@','') %> + expect(response).to be_successful + end + end + + describe "POST /create" do + context "with valid parameters" do + it "creates a new <%= class_name %>" do + expect { + post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes } + }.to change(<%= class_name %>, :count).by(1) + end + + it "redirects to the created <%= ns_file_name %>" do + post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes } + expect(response).to redirect_to(<%= show_helper.gsub("\@#{file_name}", class_name+".last") %>) + end + end + + context "with invalid parameters" do + it "does not create a new <%= class_name %>" do + expect { + post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes } + }.to change(<%= class_name %>, :count).by(0) + end + + it "renders a successful response (i.e. to display the 'new' template)" do + post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes } + expect(response).to be_successful + end + end + end + + describe "PATCH /update" do + context "with valid parameters" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: new_attributes } + <%= file_name %>.reload + skip("Add assertions for updated state") + end + + it "redirects to the <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: new_attributes } + <%= file_name %>.reload + expect(response).to redirect_to(<%= singular_table_name %>_url(<%= file_name %>)) + end + end + + context "with invalid parameters" do + it "renders a successful response (i.e. to display the 'edit' template)" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: invalid_attributes } + expect(response).to be_successful + end + end + end + + describe "DELETE /destroy" do + it "destroys the requested <%= ns_file_name %>" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + expect { + delete <%= show_helper.tr('@', '') %> + }.to change(<%= class_name %>, :count).by(-1) + end + + it "redirects to the <%= table_name %> list" do + <%= file_name %> = <%= class_name %>.create! valid_attributes + delete <%= show_helper.tr('@', '') %> + expect(response).to redirect_to(<%= index_helper %>_url) + end + end +end +<% end -%> diff --git a/lib/templates/rspec/scaffold/routing_spec.rb b/lib/templates/rspec/scaffold/routing_spec.rb new file mode 100644 index 00000000..7b35a6a8 --- /dev/null +++ b/lib/templates/rspec/scaffold/routing_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require "rails_helper" + +<% module_namespacing do -%> +RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:routing) %> do + describe "routing" do +<% unless options[:singleton] -%> + it "routes to #index" do + expect(get: "/<%= ns_table_name %>").to route_to("<%= ns_table_name %>#index") + end + +<% end -%> +<% unless options[:api] -%> + it "routes to #new" do + expect(get: "/<%= ns_table_name %>/new").to route_to("<%= ns_table_name %>#new") + end + +<% end -%> + it "routes to #show" do + expect(get: "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#show", id: "1") + end + +<% unless options[:api] -%> + it "routes to #edit" do + expect(get: "/<%= ns_table_name %>/1/edit").to route_to("<%= ns_table_name %>#edit", id: "1") + end + +<% end -%> + + it "routes to #create" do + expect(post: "/<%= ns_table_name %>").to route_to("<%= ns_table_name %>#create") + end + + it "routes to #update via PUT" do + expect(put: "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#update", id: "1") + end + + it "routes to #update via PATCH" do + expect(patch: "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#update", id: "1") + end + + it "routes to #destroy" do + expect(delete: "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#destroy", id: "1") + end + end +end +<% end -%> diff --git a/lib/templates/rspec/scaffold/show_spec.rb b/lib/templates/rspec/scaffold/show_spec.rb new file mode 100644 index 00000000..c6f13948 --- /dev/null +++ b/lib/templates/rspec/scaffold/show_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +<% 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 + @<%= ns_file_name %> = assign(:<%= ns_file_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 -%> +<% if !output_attributes.empty? -%> + )) +<% end -%> + end + + it "renders attributes in

" do + render +<% for attribute in output_attributes -%> + expect(rendered).to match(/<%= raw_value_for(attribute) %>/) +<% end -%> + end +end diff --git a/lib/templates/rspec/system/system_spec.rb b/lib/templates/rspec/system/system_spec.rb new file mode 100644 index 00000000..3e606d7e --- /dev/null +++ b/lib/templates/rspec/system/system_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:system) %> do + before do + driven_by(:rack_test) + end + + pending "add some scenarios (or delete) #{__FILE__}" +end diff --git a/lib/templates/rspec/view/view_spec.rb b/lib/templates/rspec/view/view_spec.rb new file mode 100644 index 00000000..580f5e8d --- /dev/null +++ b/lib/templates/rspec/view/view_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require 'rails_helper' + +RSpec.describe "<%= file_path %>/<%= @action %>", <%= type_metatag(:view) %> do + pending "add some examples to (or delete) #{__FILE__}" +end