|
| 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 | +RSpec.describe PostsController, type: :controller do |
| 27 | + |
| 28 | + # This should return the minimal set of attributes required to create a valid |
| 29 | + # Post. As you add validations to Post, be sure to |
| 30 | + # adjust the attributes here as well. |
| 31 | + let(:user){ |
| 32 | + User.save(name: "hello", username: "aaaa", email: "[email protected]", password: "ahahqq1234") |
| 33 | + } |
| 34 | + let(:valid_attributes) { |
| 35 | + Post.new() |
| 36 | + } |
| 37 | + |
| 38 | + let(:invalid_attributes) { |
| 39 | + skip("Add a hash of attributes invalid for your model") |
| 40 | + } |
| 41 | + |
| 42 | + # This should return the minimal set of values that should be in the session |
| 43 | + # in order to pass any filters (e.g. authentication) defined in |
| 44 | + # PostsController. Be sure to keep this updated too. |
| 45 | + let(:valid_session) { {} } |
| 46 | + |
| 47 | + describe "GET #index" do |
| 48 | + it "returns a success response" do |
| 49 | + post = Post.create! valid_attributes |
| 50 | + get :index, params: {}, session: valid_session |
| 51 | + expect(response).to be_successful |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + describe "GET #show" do |
| 56 | + it "returns a success response" do |
| 57 | + post = Post.create! valid_attributes |
| 58 | + get :show, params: {id: post.to_param}, session: valid_session |
| 59 | + expect(response).to be_successful |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe "POST #create" do |
| 64 | + context "with valid params" do |
| 65 | + it "creates a new Post" do |
| 66 | + expect { |
| 67 | + post :create, params: {post: valid_attributes}, session: valid_session |
| 68 | + }.to change(Post, :count).by(1) |
| 69 | + end |
| 70 | + |
| 71 | + it "renders a JSON response with the new post" do |
| 72 | + |
| 73 | + post :create, params: {post: valid_attributes}, session: valid_session |
| 74 | + expect(response).to have_http_status(:created) |
| 75 | + expect(response.content_type).to eq('application/json') |
| 76 | + expect(response.location).to eq(post_url(Post.last)) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + context "with invalid params" do |
| 81 | + it "renders a JSON response with errors for the new post" do |
| 82 | + |
| 83 | + post :create, params: {post: invalid_attributes}, session: valid_session |
| 84 | + expect(response).to have_http_status(:unprocessable_entity) |
| 85 | + expect(response.content_type).to eq('application/json') |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + describe "PUT #update" do |
| 91 | + context "with valid params" do |
| 92 | + let(:new_attributes) { |
| 93 | + skip("Add a hash of attributes valid for your model") |
| 94 | + } |
| 95 | + |
| 96 | + it "updates the requested post" do |
| 97 | + post = Post.create! valid_attributes |
| 98 | + put :update, params: {id: post.to_param, post: new_attributes}, session: valid_session |
| 99 | + post.reload |
| 100 | + skip("Add assertions for updated state") |
| 101 | + end |
| 102 | + |
| 103 | + it "renders a JSON response with the post" do |
| 104 | + post = Post.create! valid_attributes |
| 105 | + |
| 106 | + put :update, params: {id: post.to_param, post: valid_attributes}, session: valid_session |
| 107 | + expect(response).to have_http_status(:ok) |
| 108 | + expect(response.content_type).to eq('application/json') |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + context "with invalid params" do |
| 113 | + it "renders a JSON response with errors for the post" do |
| 114 | + post = Post.create! valid_attributes |
| 115 | + |
| 116 | + put :update, params: {id: post.to_param, post: invalid_attributes}, session: valid_session |
| 117 | + expect(response).to have_http_status(:unprocessable_entity) |
| 118 | + expect(response.content_type).to eq('application/json') |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + describe "DELETE #destroy" do |
| 124 | + it "destroys the requested post" do |
| 125 | + post = Post.create! valid_attributes |
| 126 | + expect { |
| 127 | + delete :destroy, params: {id: post.to_param}, session: valid_session |
| 128 | + }.to change(Post, :count).by(-1) |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | +end |
0 commit comments