Skip to content

Commit 7d56866

Browse files
committed
test: add RSpec specs for RiotApiService
- Test API initialization and configuration - Add specs for summoner lookup endpoints - Test error handling (NotFound, RateLimit, Unauthorized) - Verify region mapping functionality - Mock external API requests with WebMock
1 parent 7ff95ef commit 7d56866

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe RiotApiService do
4+
let(:api_key) { 'test-api-key' }
5+
let(:service) { described_class.new(api_key: api_key) }
6+
7+
describe '#initialize' do
8+
it 'requires an API key' do
9+
expect { described_class.new }.not_to raise_error
10+
end
11+
12+
it 'accepts custom API key' do
13+
expect(service.instance_variable_get(:@api_key)).to eq(api_key)
14+
end
15+
end
16+
17+
describe '#get_summoner_by_name' do
18+
let(:summoner_name) { 'TestPlayer' }
19+
let(:region) { 'BR' }
20+
21+
it 'fetches summoner data' do
22+
stub_request(:get, /br1.api.riotgames.com/)
23+
.to_return(
24+
status: 200,
25+
body: {
26+
id: 'summoner-id',
27+
puuid: 'puuid-123',
28+
name: 'TestPlayer',
29+
summonerLevel: 150,
30+
profileIconId: 4567
31+
}.to_json,
32+
headers: { 'Content-Type' => 'application/json' }
33+
)
34+
35+
result = service.get_summoner_by_name(summoner_name: summoner_name, region: region)
36+
37+
expect(result).to include(
38+
summoner_id: 'summoner-id',
39+
puuid: 'puuid-123',
40+
summoner_name: 'TestPlayer'
41+
)
42+
end
43+
44+
it 'raises NotFoundError for non-existent summoner' do
45+
stub_request(:get, /br1.api.riotgames.com/)
46+
.to_return(status: 404)
47+
48+
expect {
49+
service.get_summoner_by_name(summoner_name: summoner_name, region: region)
50+
}.to raise_error(RiotApiService::NotFoundError)
51+
end
52+
53+
it 'raises RateLimitError when rate limited' do
54+
stub_request(:get, /br1.api.riotgames.com/)
55+
.to_return(status: 429, headers: { 'Retry-After' => '120' })
56+
57+
expect {
58+
service.get_summoner_by_name(summoner_name: summoner_name, region: region)
59+
}.to raise_error(RiotApiService::RateLimitError)
60+
end
61+
end
62+
63+
describe 'region mapping' do
64+
it 'maps BR to correct platform' do
65+
expect(service.send(:platform_for_region, 'BR')).to eq('BR1')
66+
end
67+
68+
it 'raises error for unknown region' do
69+
expect {
70+
service.send(:platform_for_region, 'INVALID')
71+
}.to raise_error(RiotApiService::RiotApiError, /Unknown region/)
72+
end
73+
end
74+
end

0 commit comments

Comments
 (0)