Reduce expectations for `RSpec/MultipleExpectations` cop in `api/v1/accounts/relationships` spec (#27879)

main
Matt Jankowski 2023-11-16 04:55:50 -05:00 committed by GitHub
parent c1f93def40
commit 3f0c1566c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 76 additions and 54 deletions

View File

@ -27,12 +27,16 @@ describe 'GET /api/v1/accounts/relationships' do
it 'returns JSON with correct data', :aggregate_failures do it 'returns JSON with correct data', :aggregate_failures do
subject subject
json = body_as_json expect(response)
.to have_http_status(200)
expect(response).to have_http_status(200) expect(body_as_json)
expect(json).to be_a Enumerable .to be_an(Enumerable)
expect(json.first[:following]).to be true .and have_attributes(
expect(json.first[:followed_by]).to be false first: include(
following: true,
followed_by: false
)
)
end end
end end
@ -40,18 +44,19 @@ describe 'GET /api/v1/accounts/relationships' do
let(:params) { { id: [simon.id, lewis.id, bob.id] } } let(:params) { { id: [simon.id, lewis.id, bob.id] } }
context 'when there is returned JSON data' do context 'when there is returned JSON data' do
let(:json) { body_as_json }
context 'with default parameters' do context 'with default parameters' do
it 'returns an enumerable json with correct elements, excluding suspended accounts', :aggregate_failures do it 'returns an enumerable json with correct elements, excluding suspended accounts', :aggregate_failures do
subject subject
expect(response).to have_http_status(200) expect(response)
expect(json).to be_a Enumerable .to have_http_status(200)
expect(json.size).to eq 2 expect(body_as_json)
.to be_an(Enumerable)
expect_simon_item_one .and have_attributes(
expect_lewis_item_two size: 2,
first: include(simon_item),
second: include(lewis_item)
)
end end
end end
@ -61,62 +66,75 @@ describe 'GET /api/v1/accounts/relationships' do
it 'returns an enumerable json with correct elements, including suspended accounts', :aggregate_failures do it 'returns an enumerable json with correct elements, including suspended accounts', :aggregate_failures do
subject subject
expect(response).to have_http_status(200) expect(response)
expect(json).to be_a Enumerable .to have_http_status(200)
expect(json.size).to eq 3 expect(body_as_json)
.to be_an(Enumerable)
expect_simon_item_one .and have_attributes(
expect_lewis_item_two size: 3,
expect_bob_item_three first: include(simon_item),
second: include(lewis_item),
third: include(bob_item)
)
end end
end end
def expect_simon_item_one def simon_item
expect(json.first[:id]).to eq simon.id.to_s {
expect(json.first[:following]).to be true id: simon.id.to_s,
expect(json.first[:showing_reblogs]).to be true following: true,
expect(json.first[:followed_by]).to be false showing_reblogs: true,
expect(json.first[:muting]).to be false followed_by: false,
expect(json.first[:requested]).to be false muting: false,
expect(json.first[:domain_blocking]).to be false requested: false,
domain_blocking: false,
}
end end
def expect_lewis_item_two def lewis_item
expect(json.second[:id]).to eq lewis.id.to_s {
expect(json.second[:following]).to be false id: lewis.id.to_s,
expect(json.second[:showing_reblogs]).to be false following: false,
expect(json.second[:followed_by]).to be true showing_reblogs: false,
expect(json.second[:muting]).to be false followed_by: true,
expect(json.second[:requested]).to be false muting: false,
expect(json.second[:domain_blocking]).to be false requested: false,
domain_blocking: false,
}
end end
def expect_bob_item_three def bob_item
expect(json.third[:id]).to eq bob.id.to_s {
expect(json.third[:following]).to be false id: bob.id.to_s,
expect(json.third[:showing_reblogs]).to be false following: false,
expect(json.third[:followed_by]).to be false showing_reblogs: false,
expect(json.third[:muting]).to be false followed_by: false,
expect(json.third[:requested]).to be false muting: false,
expect(json.third[:domain_blocking]).to be false requested: false,
domain_blocking: false,
}
end end
end end
it 'returns JSON with correct data on previously cached requests' do it 'returns JSON with correct data on previously cached requests' do
# Initial request including multiple accounts in params # Initial request including multiple accounts in params
get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id, lewis.id] } get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id, lewis.id] }
expect(body_as_json.size).to eq(2) expect(body_as_json)
.to have_attributes(size: 2)
# Subsequent request with different id, should override cache from first request # Subsequent request with different id, should override cache from first request
get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id] } get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id] }
expect(response).to have_http_status(200) expect(response)
.to have_http_status(200)
expect(body_as_json) expect(body_as_json)
.to be_an(Enumerable) .to be_an(Enumerable)
.and have_attributes( .and have_attributes(
size: 1, size: 1,
first: hash_including( first: include(
following: true, following: true,
showing_reblogs: true showing_reblogs: true
) )
@ -129,13 +147,17 @@ describe 'GET /api/v1/accounts/relationships' do
get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id] } get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id] }
expect(response).to have_http_status(200) expect(response)
.to have_http_status(200)
json = body_as_json expect(body_as_json)
.to be_an(Enumerable)
expect(json).to be_a Enumerable .and have_attributes(
expect(json.first[:following]).to be false first: include(
expect(json.first[:showing_reblogs]).to be false following: false,
showing_reblogs: false
)
)
end end
end end
end end