Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#58comment delete api #70

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
infra/env/backend.env
.idea
10 changes: 10 additions & 0 deletions backend/app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def create
end
end

def destroy
comment = Comment.find_by!(id: params[:id], memo_id: params[:memo_id])

if comment.destroy
head :no_content
else
render json: { errors: comment.errors.full_messages }, status: :unprocessable_entity
end
end

private

def comment_params
Expand Down
2 changes: 1 addition & 1 deletion backend/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
resources :memos, only: %i[index show create update destroy] do
resources :comments, only: %i[create]
resources :comments, only: %i[create destroy]
end
end
62 changes: 62 additions & 0 deletions backend/spec/requests/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,66 @@
end
end
end

describe 'DELETE /memos/:memo_id/comments/:id' do
context 'コメントが存在する場合' do
let!(:memo) { create(:memo) }
let!(:comment) { create(:comment, memo: memo) }

it 'コメントが削除され、204になる' do
aggregate_failures do
expect do
delete "/memos/#{memo.id}/comments/#{comment.id}", as: :json
end.to change(Comment, :count).by(-1)
expect(response).to have_http_status(:no_content)
end
end
end

context 'コメントが存在しない場合' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO

memoが存在しない場合もテストしたいですね!
また、コメントが存在しない場合は、以下のようなリクエストが適切かなと思います!
delete "/memos/#{memo.id}/comments/0", as: :json

let!(:memo) { create(:memo) }

it '404が返ることを確認する' do
aggregate_failures do
expect do
delete "/memos/#{memo.id}/comments/0", as: :json
end.not_to change(Comment, :count)
expect(response).to have_http_status(:not_found)
end
end
end

context 'メモが存在しない場合' do
let!(:memo) { create(:memo) }
let!(:comment) { create(:comment, memo: memo) }

it '404が返ることを確認する' do
aggregate_failures do
expect do
delete "/memos/0/comments/#{comment.id}", as: :json
end.not_to change(Comment, :count)
expect(response).to have_http_status(:not_found)
end
end
end

context 'コメントの削除に失敗した場合' do
let!(:memo) { create(:memo) }
let!(:comment) { create(:comment, memo: memo) }

before do
allow(Comment).to receive(:find_by).and_return(comment)
allow(comment).to receive(:destroy).and_return(false)
end

it '422が返ることを確認する' do
aggregate_failures do
expect do
delete "/memos/#{memo.id}/comments/#{comment.id}", as: :json
end.not_to change(Comment, :count)
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
end
end
Loading