|
| 1 | +# frozen_string_literal:true |
1 | 2 |
|
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +# This spec was generated by rspec-rails when you ran the scaffold generator. |
| 6 | +# It demonstrates how one might use RSpec to specify the controller code that |
| 7 | +# was generated by Rails when you ran the scaffold generator. |
| 8 | +# |
| 9 | +# It assumes that the implementation code is generated by the rails scaffold |
| 10 | +# generator. If you are using any extension libraries to generate different |
| 11 | +# controller code, this generated spec may or may not pass. |
| 12 | +# |
| 13 | +# It only uses APIs available in rails and/or rspec-rails. There are a number |
| 14 | +# of tools you can use to make these specs even more expressive, but we're |
| 15 | +# sticking to rails and rspec-rails APIs to keep things simple and stable. |
| 16 | + |
| 17 | +<% module_namespacing do -%> |
| 18 | +RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:request) %> do |
| 19 | + # This should return the minimal set of attributes required to create a valid |
| 20 | + # <%= class_name %>. As you add validations to <%= class_name %>, be sure to |
| 21 | + # adjust the attributes here as well. |
| 22 | + let(:valid_attributes) { |
| 23 | + skip("Add a hash of attributes valid for your model") |
| 24 | + } |
| 25 | +
|
| 26 | + let(:invalid_attributes) { |
| 27 | + skip("Add a hash of attributes invalid for your model") |
| 28 | + } |
| 29 | +
|
| 30 | +<% unless options[:singleton] -%> |
| 31 | + describe "GET /index" do |
| 32 | + it "renders a successful response" do |
| 33 | + <%= class_name %>.create! valid_attributes |
| 34 | + get <%= "#{index_helper}_url" %> |
| 35 | + expect(response).to be_successful |
| 36 | + end |
| 37 | + end |
| 38 | +<% end -%> |
| 39 | +
|
| 40 | + describe "GET /show" do |
| 41 | + it "renders a successful response" do |
| 42 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 43 | + get <%= "#{singular_table_name}_url(#{file_name})" %> |
| 44 | + expect(response).to be_successful |
| 45 | + end |
| 46 | + end |
| 47 | +
|
| 48 | + describe "GET /new" do |
| 49 | + it "renders a successful response" do |
| 50 | + get <%= "#{new_helper}" %> |
| 51 | + expect(response).to be_successful |
| 52 | + end |
| 53 | + end |
| 54 | +
|
| 55 | + describe "GET /edit" do |
| 56 | + it "render a successful response" do |
| 57 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 58 | + get <%= "edit_#{singular_table_name}_url(#{file_name})" %> |
| 59 | + expect(response).to be_successful |
| 60 | + end |
| 61 | + end |
| 62 | +
|
| 63 | + describe "POST /create" do |
| 64 | + context "with valid parameters" do |
| 65 | + it "creates a new <%= class_name %>" do |
| 66 | + expect { |
| 67 | + post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes } |
| 68 | + }.to change(<%= class_name %>, :count).by(1) |
| 69 | + end |
| 70 | +
|
| 71 | + it "redirects to the created <%= ns_file_name %>" do |
| 72 | + post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes } |
| 73 | + expect(response).to redirect_to(<%= singular_table_name %>_url(<%= class_name %>.last)) |
| 74 | + end |
| 75 | + end |
| 76 | +
|
| 77 | + context "with invalid parameters" do |
| 78 | + it "does not create a new <%= class_name %>" do |
| 79 | + expect { |
| 80 | + post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: invalid_attributes } |
| 81 | + }.to change(<%= class_name %>, :count).by(0) |
| 82 | + end |
| 83 | +
|
| 84 | + it "renders a successful response (i.e. to display the 'new' template)" do |
| 85 | + post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: invalid_attributes } |
| 86 | + expect(response).to be_successful |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | +
|
| 91 | + describe "PATCH /update" do |
| 92 | + context "with valid parameters" do |
| 93 | + let(:new_attributes) { |
| 94 | + skip("Add a hash of attributes valid for your model") |
| 95 | + } |
| 96 | +
|
| 97 | + it "updates the requested <%= ns_file_name %>" do |
| 98 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 99 | + patch <%= "#{singular_table_name}_url(#{file_name})" %>, params: { <%= singular_table_name %>: new_attributes } |
| 100 | + <%= file_name %>.reload |
| 101 | + skip("Add assertions for updated state") |
| 102 | + end |
| 103 | +
|
| 104 | + it "redirects to the <%= ns_file_name %>" do |
| 105 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 106 | + patch <%= "#{singular_table_name}_url(#{file_name})" %>, params: { <%= singular_table_name %>: new_attributes } |
| 107 | + <%= file_name %>.reload |
| 108 | + expect(response).to redirect_to(<%= singular_table_name %>_url(<%= file_name %>)) |
| 109 | + end |
| 110 | + end |
| 111 | +
|
| 112 | + context "with invalid parameters" do |
| 113 | + it "renders a successful response (i.e. to display the 'edit' template)" do |
| 114 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 115 | + patch <%= "#{singular_table_name}_url(#{file_name})" %>, params: { <%= singular_table_name %>: invalid_attributes } |
| 116 | + expect(response).to be_successful |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | +
|
| 121 | + describe "DELETE /destroy" do |
| 122 | + it "destroys the requested <%= ns_file_name %>" do |
| 123 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 124 | + expect { |
| 125 | + delete <%= "#{singular_table_name}_url(#{file_name})" %> |
| 126 | + }.to change(<%= class_name %>, :count).by(-1) |
| 127 | + end |
| 128 | +
|
| 129 | + it "redirects to the <%= table_name %> list" do |
| 130 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 131 | + delete <%= "#{singular_table_name}_url(#{file_name})" %> |
| 132 | + expect(response).to redirect_to(<%= "#{index_helper}_url" %>) |
| 133 | + end |
| 134 | + end |
| 135 | +end |
| 136 | +<% end -%> |
0 commit comments