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