diff options
6 files changed, 52 insertions, 4 deletions
diff --git a/spec/factories/company.rb b/spec/factories/company.rb index e4ae7d6..df805ca 100644 --- a/spec/factories/company.rb +++ b/spec/factories/company.rb @@ -3,7 +3,7 @@ FactoryBot.define do factory :company, class: 'Company' do name { Faker::Company.name } - short_name { Faker::Lorem.word } + short_name { SecureRandom.hex(6) } country { ISO3166::Country.codes.sample } end end diff --git a/spec/requests/companies_controller/index_companies_controller_spec.rb b/spec/requests/companies_controller/index_companies_controller_spec.rb new file mode 100644 index 0000000..c8fb439 --- /dev/null +++ b/spec/requests/companies_controller/index_companies_controller_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/companies', type: :request do + before(:each) do + create_list(:company, 12) + end + + it_behaves_like 'a GET index request' do + let(:headers) { {} } + let(:companies) { create_list(:company, 12) } + let(:route) { '/api/companies' } + let(:pagination_size) { 10 } + end +end diff --git a/spec/requests/companies_controller/show_companies_controller_spec.rb b/spec/requests/companies_controller/show_companies_controller_spec.rb new file mode 100644 index 0000000..fc5bbd0 --- /dev/null +++ b/spec/requests/companies_controller/show_companies_controller_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/companies/:short_name', type: :request do + it_behaves_like 'a GET resource request' do + let(:headers) { {} } + let(:company) { create(:company) } + let(:route) { "/api/companies/#{company.short_name}" } + let(:invalid_route) { "/api/companies/#{Faker::Lorem.word}" } + let(:expected_text) { [company.name, company.id.to_s, company.short_name, company.country] } + end +end diff --git a/spec/support/shared_examples/requests/get_index_request.rb b/spec/support/shared_examples/requests/get_index_request.rb new file mode 100644 index 0000000..2875b28 --- /dev/null +++ b/spec/support/shared_examples/requests/get_index_request.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'a GET index request' do + it 'returns a 200 http status and the requested resources' do + get(route, headers:) + expect(response).to have_http_status(200) + resources = JSON.parse(response.body) + expect(resources['data'].length).to eq(pagination_size) + end +end
\ No newline at end of file diff --git a/spec/support/shared_examples/requests/get_request.rb b/spec/support/shared_examples/requests/get_request.rb index c402196..d58da67 100644 --- a/spec/support/shared_examples/requests/get_request.rb +++ b/spec/support/shared_examples/requests/get_request.rb @@ -1,11 +1,20 @@ # frozen_string_literal: true -RSpec.shared_examples 'a GET request' do +RSpec.shared_examples 'a GET resource request' do |sole_route: false| context 'for an existing resource' do - it 'returns a 200 status and the requested resource(s)'do + it 'returns a 200 status and the requested resource(s)' do get(route, headers:) expect(response).to have_http_status(200) expect(response.body).to include_strings(expected_text) end end + + unless sole_route + context 'for a nonexistent resource' do + it 'returns a 404 http status' do + get(invalid_route, headers:) + expect(response).to have_http_status(404) + end + end + end end diff --git a/spec/support/shared_examples/requests/put_request.rb b/spec/support/shared_examples/requests/put_request.rb index d329b78..a85d842 100644 --- a/spec/support/shared_examples/requests/put_request.rb +++ b/spec/support/shared_examples/requests/put_request.rb @@ -17,7 +17,7 @@ RSpec.shared_examples 'a PUT request' do |account: false| end end - if account + unless account context 'to an nonexistent resource' do it 'returns a 404 status' do put(wrong_route, params:, headers:) |