|
| 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}_path" %> |
| 35 | + expect(response).to be_successful |
| 36 | + end |
| 37 | + end |
| 38 | +<% end -%> |
| 39 | + |
| 40 | + describe "GET /new" do |
| 41 | + it "renders a successful response" do |
| 42 | + get <%= "#{new_helper}_path" %> |
| 43 | + expect(response).to be_successful |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe "POST /create" do |
| 48 | + context "with valid parameters" do |
| 49 | + it "creates a new <%= class_name %>" do |
| 50 | + expect { |
| 51 | + post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes } |
| 52 | + }.to change(<%= class_name %>, :count).by(1) |
| 53 | + end |
| 54 | + |
| 55 | + it "redirects to the created <%= ns_file_name %>" do |
| 56 | + expect { |
| 57 | + post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes } |
| 58 | + }.to redirect_to("<%= ns_table_name %>/#{<%= class_name %>.last}") |
| 59 | + end |
| 60 | + end |
| 61 | +
|
| 62 | + context "with invalid parameters" do |
| 63 | + it "does not create a new <%= class_name %>" do |
| 64 | + expect { |
| 65 | + post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: invalid_attributes } |
| 66 | + }.not_to change(<%= class_name %>, :count).by(1) |
| 67 | + end |
| 68 | + |
| 69 | + it "renders a successful response (i.e. to display the 'new' template)" do |
| 70 | + post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: invalid_attributes } |
| 71 | + expect(response).to be_successful |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe "PUT /update" do |
| 77 | + context "with valid parameters" do |
| 78 | + let(:new_attributes) { |
| 79 | + skip("Add a hash of attributes valid for your model") |
| 80 | + } |
| 81 | + |
| 82 | + it "updates the requested <%= ns_file_name %>" do |
| 83 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 84 | + put "/<%= ns_table_name %>/<%= file_name %>.id", params: { <%= ns_file_name %>: new_attributes } |
| 85 | + <%= file_name %>.reload |
| 86 | + skip("Add assertions for updated state") |
| 87 | + end |
| 88 | + |
| 89 | + it "redirects to the <%= ns_file_name %>" do |
| 90 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 91 | + put "/<%= ns_table_name %>/<%= file_name %>.id", params: { <%= ns_file_name %>: new_attributes } |
| 92 | + <%= file_name %>.reload |
| 93 | + expect(response).to redirect_to("<%= ns_table_name %>/#{<%= file_name %>.id}") |
| 94 | + end |
| 95 | + end |
| 96 | +
|
| 97 | + context "with invalid parameters" do |
| 98 | + it "renders a successful response (i.e. to display the 'edit' template)" do |
| 99 | + put "/<%= ns_table_name %>/<%= file_name %>.id", params: { <%= ns_file_name %>: invalid_attributes } |
| 100 | + expect(response).to be_successful |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | +
|
| 105 | + describe "DELETE /destroy" do |
| 106 | + it "destroys the requested <%= ns_file_name %>" do |
| 107 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 108 | + expect { |
| 109 | + delete delete: "/<%= ns_table_name %>/<%= file_name %>.id" |
| 110 | + }.to change(<%= class_name %>, :count).by(-1) |
| 111 | + end |
| 112 | +
|
| 113 | + it "redirects to the <%= table_name %> list" do |
| 114 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 115 | + delete: "/<%= ns_table_name %>/<%= file_name %>.id" |
| 116 | + expect(response).to redirect_to("<%= ns_table_name %>/#{<%= class_name %>.last}") |
| 117 | + end |
| 118 | + end |
| 119 | +end |
| 120 | +<% end -%> |
0 commit comments