|
| 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 specify 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 | +# Compared to earlier versions of this generator, there is very limited use of |
| 16 | +# stubs and message expectations in this spec. Stubs are only used when there |
| 17 | +# is no simpler way to get a handle on the object needed for the example. |
| 18 | +# Message expectations are only used when there is no simpler way to specify |
| 19 | +# that an instance is receiving a specific message. |
| 20 | +# |
| 21 | +# Also compared to earlier versions of this generator, there are no longer any |
| 22 | +# expectations of assigns and templates rendered. These features have been |
| 23 | +# removed from Rails core in Rails 5, but can be added back in via the |
| 24 | +# `rails-controller-testing` gem. |
| 25 | + |
| 26 | +<% module_namespacing do -%> |
| 27 | +RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:controller) %> do |
| 28 | +
|
| 29 | + # This should return the minimal set of attributes required to create a valid |
| 30 | + # <%= class_name %>. As you add validations to <%= class_name %>, be sure to |
| 31 | + # adjust the attributes here as well. |
| 32 | + let(:valid_attributes) { |
| 33 | + skip("Add a hash of attributes valid for your model") |
| 34 | + } |
| 35 | +
|
| 36 | + let(:invalid_attributes) { |
| 37 | + skip("Add a hash of attributes invalid for your model") |
| 38 | + } |
| 39 | +
|
| 40 | + # This should return the minimal set of values that should be in the session |
| 41 | + # in order to pass any filters (e.g. authentication) defined in |
| 42 | + # <%= controller_class_name %>Controller. Be sure to keep this updated too. |
| 43 | + let(:valid_session) { {} } |
| 44 | +
|
| 45 | +<% unless options[:singleton] -%> |
| 46 | + describe "GET #index" do |
| 47 | + it "returns a success response" do |
| 48 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 49 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 50 | + get :index, {}, valid_session |
| 51 | +<% else -%> |
| 52 | + get :index, params: {}, session: valid_session |
| 53 | +<% end -%> |
| 54 | + expect(response).to be_success |
| 55 | + end |
| 56 | + end |
| 57 | +
|
| 58 | +<% end -%> |
| 59 | + describe "GET #show" do |
| 60 | + it "returns a success response" do |
| 61 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 62 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 63 | + get :show, {:id => <%= file_name %>.to_param}, valid_session |
| 64 | +<% else -%> |
| 65 | + get :show, params: {id: <%= file_name %>.to_param}, session: valid_session |
| 66 | +<% end -%> |
| 67 | + expect(response).to be_success |
| 68 | + end |
| 69 | + end |
| 70 | +
|
| 71 | + describe "POST #create" do |
| 72 | + context "with valid params" do |
| 73 | + it "creates a new <%= class_name %>" do |
| 74 | + expect { |
| 75 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 76 | + post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session |
| 77 | +<% else -%> |
| 78 | + post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session |
| 79 | +<% end -%> |
| 80 | + }.to change(<%= class_name %>, :count).by(1) |
| 81 | + end |
| 82 | +
|
| 83 | + it "renders a JSON response with the new <%= ns_file_name %>" do |
| 84 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 85 | + post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session |
| 86 | +<% else %> |
| 87 | + post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session |
| 88 | +<% end -%> |
| 89 | + expect(response).to have_http_status(:created) |
| 90 | + expect(response.content_type).to eq('application/json') |
| 91 | + expect(response.location).to eq(<%= ns_file_name %>_url(<%= class_name %>.last)) |
| 92 | + end |
| 93 | + end |
| 94 | +
|
| 95 | + context "with invalid params" do |
| 96 | + it "renders a JSON response with errors for the new <%= ns_file_name %>" do |
| 97 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 98 | + post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session |
| 99 | +<% else %> |
| 100 | + post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session |
| 101 | +<% end -%> |
| 102 | + expect(response).to have_http_status(:unprocessable_entity) |
| 103 | + expect(response.content_type).to eq('application/json') |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +
|
| 108 | + describe "PUT #update" do |
| 109 | + context "with valid params" do |
| 110 | + let(:new_attributes) { |
| 111 | + skip("Add a hash of attributes valid for your model") |
| 112 | + } |
| 113 | +
|
| 114 | + it "updates the requested <%= ns_file_name %>" do |
| 115 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 116 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 117 | + put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => new_attributes}, valid_session |
| 118 | +<% else -%> |
| 119 | + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session |
| 120 | +<% end -%> |
| 121 | + <%= file_name %>.reload |
| 122 | + skip("Add assertions for updated state") |
| 123 | + end |
| 124 | +
|
| 125 | + it "renders a JSON response with the <%= ns_file_name %>" do |
| 126 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 127 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 128 | + put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session |
| 129 | +<% else %> |
| 130 | + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session |
| 131 | +<% end -%> |
| 132 | + expect(response).to have_http_status(:ok) |
| 133 | + expect(response.content_type).to eq('application/json') |
| 134 | + end |
| 135 | + end |
| 136 | +
|
| 137 | + context "with invalid params" do |
| 138 | + it "renders a JSON response with errors for the <%= ns_file_name %>" do |
| 139 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 140 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 141 | + put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session |
| 142 | +<% else %> |
| 143 | + put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session |
| 144 | +<% end -%> |
| 145 | + expect(response).to have_http_status(:unprocessable_entity) |
| 146 | + expect(response.content_type).to eq('application/json') |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | +
|
| 151 | + describe "DELETE #destroy" do |
| 152 | + it "destroys the requested <%= ns_file_name %>" do |
| 153 | + <%= file_name %> = <%= class_name %>.create! valid_attributes |
| 154 | + expect { |
| 155 | +<% if RUBY_VERSION < '1.9.3' -%> |
| 156 | + delete :destroy, {:id => <%= file_name %>.to_param}, valid_session |
| 157 | +<% else -%> |
| 158 | + delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session |
| 159 | +<% end -%> |
| 160 | + }.to change(<%= class_name %>, :count).by(-1) |
| 161 | + end |
| 162 | + end |
| 163 | +
|
| 164 | +end |
| 165 | +<% end -%> |
0 commit comments