ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

151 lines
4.0 KiB

  1. require 'rails_helper'
  2. RSpec.describe 'TheatreComments', type: :request do
  3. def sign_in_as(user)
  4. allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
  5. end
  6. describe 'GET /theatres/:theatre_id/comments' do
  7. let(:theatre) { create(:theatre) }
  8. let(:other_theatre) { create(:theatre) }
  9. let(:alice) { create(:user, name: 'Alice') }
  10. let(:bob) { create(:user, name: 'Bob') }
  11. let!(:comment_3) do
  12. create(
  13. :theatre_comment,
  14. theatre: theatre,
  15. no: 3,
  16. user: alice,
  17. content: 'third comment'
  18. )
  19. end
  20. let!(:comment_1) do
  21. create(
  22. :theatre_comment,
  23. theatre: theatre,
  24. no: 1,
  25. user: alice,
  26. content: 'first comment'
  27. )
  28. end
  29. let!(:comment_2) do
  30. create(
  31. :theatre_comment,
  32. theatre: theatre,
  33. no: 2,
  34. user: bob,
  35. content: 'second comment'
  36. )
  37. end
  38. let!(:other_comment) do
  39. create(
  40. :theatre_comment,
  41. theatre: other_theatre,
  42. no: 1,
  43. user: bob,
  44. content: 'other theatre comment'
  45. )
  46. end
  47. it 'theatre_id で絞り込み、no_gt より大きいものを no 降順で返す' do
  48. get "/theatres/#{theatre.id}/comments", params: { no_gt: 1 }
  49. expect(response).to have_http_status(:ok)
  50. expect(response.parsed_body.map { |row| row['no'] }).to eq([3, 2])
  51. expect(response.parsed_body.map { |row| row['content'] }).to eq([
  52. 'third comment',
  53. 'second comment'
  54. ])
  55. end
  56. it 'user は id と name だけを含む' do
  57. get "/theatres/#{theatre.id}/comments", params: { no_gt: 1 }
  58. expect(response).to have_http_status(:ok)
  59. expect(response.parsed_body.first['user']).to eq({
  60. 'id' => alice.id,
  61. 'name' => 'Alice'
  62. })
  63. expect(response.parsed_body.first['user'].keys).to contain_exactly('id', 'name')
  64. end
  65. it 'no_gt が負数なら 0 として扱う' do
  66. get "/theatres/#{theatre.id}/comments", params: { no_gt: -100 }
  67. expect(response).to have_http_status(:ok)
  68. expect(response.parsed_body.map { |row| row['no'] }).to eq([3, 2, 1])
  69. end
  70. end
  71. describe 'POST /theatres/:theatre_id/comments' do
  72. let(:user) { create(:user, name: 'Alice') }
  73. let(:theatre) { create(:theatre, next_comment_no: 2) }
  74. before do
  75. create(
  76. :theatre_comment,
  77. theatre: theatre,
  78. no: 1,
  79. user: user,
  80. content: 'existing comment'
  81. )
  82. end
  83. it '未ログインなら 401 を返す' do
  84. expect {
  85. post "/theatres/#{theatre.id}/comments", params: { content: 'hello' }
  86. }.not_to change(TheatreComment, :count)
  87. expect(response).to have_http_status(:unauthorized)
  88. end
  89. it 'content が blank なら 422 を返す' do
  90. sign_in_as(user)
  91. expect {
  92. post "/theatres/#{theatre.id}/comments", params: { content: ' ' }
  93. }.not_to change(TheatreComment, :count)
  94. expect(response).to have_http_status(:unprocessable_entity)
  95. end
  96. it 'theatre が存在しなければ 404 を返す' do
  97. sign_in_as(user)
  98. expect {
  99. post '/theatres/999999/comments', params: { content: 'hello' }
  100. }.not_to change(TheatreComment, :count)
  101. expect(response).to have_http_status(:not_found)
  102. end
  103. it 'コメントを作成し、user を紐づけ、next_comment_no を進める' do
  104. sign_in_as(user)
  105. expect {
  106. post "/theatres/#{theatre.id}/comments", params: { content: 'new comment' }
  107. }.to change(TheatreComment, :count).by(1)
  108. expect(response).to have_http_status(:created)
  109. comment = TheatreComment.find_by!(theatre: theatre, no: 2)
  110. expect(comment.user).to eq(user)
  111. expect(comment.content).to eq('new comment')
  112. expect(theatre.reload.next_comment_no).to eq(3)
  113. expect(response.parsed_body.slice('theatre_id', 'no', 'user_id', 'content')).to eq({
  114. 'theatre_id' => theatre.id,
  115. 'no' => 2,
  116. 'user_id' => user.id,
  117. 'content' => 'new comment'
  118. })
  119. end
  120. end
  121. end