Implement the remaining 2 actions update and destroy.
update is an update process. If you can create it, it's similar, so it shouldn't get stuck.
spec/requests/v1/posts_controller.rb
...
+ describe "PUT /v1/posts#update" do
+ let(:update_param) do
+ post = create(:post)
+ update_param = attributes_for(:post, subject: "update_subject test", body: "update_body test")
+ update_param[:id] = post.id
+ update_param
+ end
+ it "Normal response code is returned" do
+ put v1_post_url({ id: update_param[:id] }), params: update_param
+ expect(response.status).to eq 200
+ end
+ it "subject,body returns correctly" do
+ put v1_post_url({ id: update_param[:id] }), params: update_param
+ json = JSON.parse(response.body)
+ expect(json["post"]["subject"]).to eq("update_subject test")
+ expect(json["post"]["body"]).to eq("update_body test")
+ end
+ it "Errors are returned when the parameter is invalid" do
+ put v1_post_url({ id: update_param[:id] }), params: { subject: "" }
+ json = JSON.parse(response.body)
+ expect(json.key?("errors")).to be true
+ end
+ it "404 response is returned when id does not exist" do
+ put v1_post_url({ id: update_param[:id] + 1 }), params: update_param
+ expect(response.status).to eq 404
+ end
+ end
...
As usual, the controller is not implemented, so the test is moss.
app/controllers/v1/posts_controller.rb
...
def update
- # TODO
+ if @post.update(post_params)
+ render json: { post: @post }
+ else
+ render json: { errors: @post.errors }
+ end
end
...
There is no need to talk about it here either. This should pass the test.
spec/requests/v1/posts_controller.rb
...
+ describe "DELETE /v1/posts#destroy" do
+ let(:delete_post) do
+ create(:post)
+ end
+ it "Normal response code is returned" do
+ delete v1_post_url({ id: delete_post.id })
+ expect(response.status).to eq 200
+ end
+ it "One less and returns" do
+ delete_post
+ expect do
+ delete v1_post_url({ id: delete_post.id })
+ end.to change { Post.count }.by(-1)
+ end
+ it "404 response is returned when id does not exist" do
+ delete v1_post_url({ id: delete_post.id + 1 })
+ expect(response.status).to eq 404
+ end
+ end
...
In the opposite of post, confirm that the number of records is reduced by 1 at Jitsugyo-ji. As a point, It means calling delete_post before expect.
As mentioned earlier, let is lazy evaluated, so if there is no delete_post before expect, a record will be created with delete_post.id
in expect and one record will be deleted with delete v1_post_url
. In other words, + 1-1 = 0 in the expect block, and there is no change in the number of records.
Therefore, a record is generated before expect, and when delete is executed in expect, it becomes -1 record.
app/controllers/v1/posts_controller.rb
...
def destroy
+ @post.destroy
+ render json: { post: @post }
end
...
This is also very simple. This time it's short, but if you include the seed to be done next, it will be too long, so that's it.
→ Building a bulletin board API with certification authorization in Rails 6 # 8 seed implementation