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

Enhance/#101 implement memo search test #109

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ GEM
reline (>= 0.3.1)
diff-lcs (1.5.0)
diffy (3.4.2)
docile (1.4.1)
drb (2.2.1)
erubi (1.13.0)
docile (1.4.1)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
Expand Down
71 changes: 71 additions & 0 deletions backend/spec/requests/memos_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,77 @@
expect(response).to have_http_status(:unauthorized)
end
end

# rubocop:disable RSpec/LetSetup
shared_context 'メモデータ' do
let!(:first_memo) { create(:memo, title: 'メモタイトル1', content: 'メモコンテンツ1') }
let!(:second_memo) { create(:memo, title: 'メモタイトル2', content: 'メモコンテンツ2') }
let!(:third_memo) { create(:memo, title: 'その他タイトル', content: 'その他のコンテンツ') }
end
# rubocop:enable RSpec/LetSetup

context 'ログイン中かつ、タイトルが指定された場合' do
include_context 'メモデータ'

before { sign_in(user) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

上記2件のデータだけだと、部分一致検索ができている、いないにも関わらず2件ヒットすると思うので

      let!(:first_memo) { create(:memo, title: 'メモタイトル1', content: 'メモコンテンツ1') }
      let!(:second_memo) { create(:memo, title: 'メモタイトル2', content: 'メモコンテンツ2') }
      let!(:third_memo) { create(:memo, title: 'その他タイトル', content: 'その他コンテンツ') }

と合致しないデータも含めた方が良いかなと思いました!


it 'タイトルで部分一致するメモが返る' do
aggregate_failures do
get '/memos', params: { title: 'メモ' }, headers: { 'Accept' => 'application/json' }
assert_request_schema_confirm
assert_response_schema_confirm(200)
expect(response.parsed_body['memos'].length).to eq(2)
result_memo_ids = response.parsed_body['memos'].pluck('id')
expect(result_memo_ids).to contain_exactly(first_memo.id, second_memo.id)
end
end
end

context 'ログイン中かつ、コンテンツが指定された場合' do
include_context 'メモデータ'
before { sign_in(user) }

it 'コンテンツで部分一致するメモが返る' do
aggregate_failures do
get '/memos', params: { content: 'メモ' }, headers: { 'Accept' => 'application/json' }
assert_request_schema_confirm
assert_response_schema_confirm(200)
expect(response.parsed_body['memos'].length).to eq(2)
result_memo_ids = response.parsed_body['memos'].pluck('id')
expect(result_memo_ids).to contain_exactly(first_memo.id, second_memo.id)
end
end
end

context 'ログイン中かつ、並び順にascが指定された場合' do
include_context 'メモデータ'
before { sign_in(user) }

it '昇順でメモが返る' do
aggregate_failures do
get '/memos', params: { order: 'asc', title: 'タイトル' }, headers: { 'Accept' => 'application/json' }
assert_request_schema_confirm
assert_response_schema_confirm(200)
result_memo_ids = response.parsed_body['memos'].pluck('id')
expect(result_memo_ids).to eq([first_memo.id, second_memo.id, third_memo.id])
end
end
end

context 'ログイン中かつ、並び順にdescが指定された場合' do
include_context 'メモデータ'
before { sign_in(user) }

it '降順でメモが返る' do
aggregate_failures do
get '/memos', params: { order: 'desc', title: 'タイトル' }, headers: { 'Accept' => 'application/json' }
assert_request_schema_confirm
assert_response_schema_confirm(200)
result_memo_ids = response.parsed_body['memos'].pluck('id')
expect(result_memo_ids).to eq([third_memo.id, second_memo.id, first_memo.id])
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

ここも同様に昇順の場合、降順の場合でcontextをわけましょう!
context 'ログイン中かつ、並び順にdescが指定された場合'

end
end

describe 'GET /memos/:id' do
Expand Down
Loading