Skip to content

Commit

Permalink
Merge pull request #29 from cameron-martin/refactor-request-spec-2
Browse files Browse the repository at this point in the history
Refactored request specs.
  • Loading branch information
intinig committed Aug 10, 2014
2 parents fe2d0e4 + 4c39b02 commit 84fab1b
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 200 deletions.
File renamed without changes.
File renamed without changes.
17 changes: 7 additions & 10 deletions spec/lol/champion_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@
expect(ChampionRequest.ancestors[1]).to eq(Request)
end

describe "get" do
let(:request) { ChampionRequest.new "api_key", "euw" }
let(:request) { ChampionRequest.new("api_key", "euw") }

describe "#get" do

context "specifying an id" do
subject do
expect(request).to receive(:perform_request).with(request.api_url("champion/266", "freeToPlay" => false)).and_return(load_fixture("champion-266", ChampionRequest.api_version, "get"))
subject { request.get(:id => 266) }

request.get :id => 266
end
before(:each) { stub_request(request, 'champion-266', 'champion/266', 'freeToPlay' => false) }

it "returns a champion" do
expect(subject).to be_a(Champion)
end
end

context "getting all" do
subject do
expect(request).to receive(:perform_request).with(request.api_url("champion", "freeToPlay" => false)).and_return(load_fixture("champion", ChampionRequest.api_version, "get"))
subject { request.get }

request.get
end
before(:each) { stub_request(request, 'champion', 'champion', 'freeToPlay' => false) }

it "returns an array" do
expect(subject).to be_a(Array)
Expand Down
17 changes: 8 additions & 9 deletions spec/lol/game_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@
expect(GameRequest.ancestors[1]).to eq(Request)
end

describe "#recent" do
let(:request) { GameRequest.new "api_key", "euw" }
let(:request) { GameRequest.new("api_key", "euw") }

subject do
expect(request.class).to receive(:get).with(request.api_url("game/by-summoner/1/recent")).and_return load_fixture('game', GameRequest.api_version, 'get')
describe "#recent" do
subject { request.recent(1) }

request.recent 1
end
before(:each) { stub_request(request, 'game', 'game/by-summoner/1/recent') }

it 'returns an array' do
expect(subject).to be_a Array
expect(subject).to be_a(Array)
end

it 'returns an array of Games' do
expect(subject.map(&:class).uniq).to eq [Game]
expect(subject.map(&:class).uniq).to eq([Game])
end

it 'fetches games from the API' do
expect(subject.size).to eq load_fixture('game', GameRequest.api_version, 'get')['games'].size
fixture = load_fixture('game', GameRequest.api_version, 'get')
expect(subject.size).to eq(fixture['games'].size)
end
end

Expand Down
9 changes: 4 additions & 5 deletions spec/lol/league_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
expect(LeagueRequest.ancestors[1]).to eq(Request)
end

let(:request) { LeagueRequest.new "api_key", "euw" }

describe "#get" do
let(:request) { LeagueRequest.new "api_key", "euw" }

subject do
expect(request.class).to receive(:get).with(request.api_url("league/by-summoner/123")).and_return(load_fixture("league", LeagueRequest.api_version, "get"))
subject { request.get(123) }

request.get(123)
end
before(:each) { stub_request(request, 'league', 'league/by-summoner/123') }

it "returns an hash of arrays of Leagues" do
expect(subject.map {|k,v| v.map(&:class).uniq}.flatten).to eq([League])
Expand Down
54 changes: 16 additions & 38 deletions spec/lol/static_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
end
end

let(:request) { StaticRequest.new "api_key", "euw" }
let(:request) { StaticRequest.new("api_key", "euw") }

describe "api_url" do
describe "#api_url" do
it "contains a static-data path component" do
expect(request.api_url("foo")).to eq("http://global.api.pvp.net/api/lol/static-data/euw/v1.2/foo?api_key=api_key")
end
Expand All @@ -32,18 +32,13 @@
end

context "without_id" do
let(:fixtures) do
fixture = endpoint == 'champion' && 'static-champion' || endpoint.dasherize
load_fixture(fixture, StaticRequest.api_version, "get")
end

subject do
expect(request).to receive(:perform_request)
.with(request.api_url("#{endpoint.dasherize}"))
.and_return(fixtures)
let(:fixture_name) { endpoint == 'champion' ? 'static-champion' : endpoint.dasherize }
let(:fixture) { load_fixture(fixture_name, StaticRequest.api_version, 'get') }

request.send(endpoint).get
end
subject { request.public_send(endpoint).get }

before(:each) { stub_request(request, fixture_name, "#{endpoint.dasherize}") }

it "returns an Array" do
expect(subject).to be_an(Array)
Expand All @@ -54,21 +49,16 @@
end

it "fetches #{endpoint} from the API" do
expect(subject.size).to eq(fixtures["data"].size)
expect(subject.size).to eq(fixture["data"].size)
end
end

context "with_id" do
let(:id) { 1 }
let(:fixtures) { load_fixture("#{endpoint.dasherize}-by-id", StaticRequest.api_version, "get") }

subject do
expect(request).to receive(:perform_request)
.with(request.api_url("#{endpoint.dasherize}/#{id}"))
.and_return(fixtures)
before(:each) { stub_request(request, "#{endpoint.dasherize}-by-id", "#{endpoint.dasherize}/#{id}") }

request.send(endpoint).get(id)
end
subject { request.public_send(endpoint).get(id) }

it "returns an OpenStruct" do
expect(subject).to be_an(OpenStruct)
Expand All @@ -78,32 +68,20 @@
end
end

describe "realm" do
let(:fixtures) { load_fixture("realm", StaticRequest.api_version, "get") }
describe "#realm" do
subject { request.realm.get }

subject do
expect(request).to receive(:perform_request)
.with(request.api_url("realm"))
.and_return(fixtures)

request.send("realm").get
end
before(:each) { stub_request(request, 'realm', 'realm') }

it "returns an OpenStruct" do
expect(subject).to be_an(OpenStruct)
end
end

describe "versions" do
let(:fixtures) { load_fixture("versions", StaticRequest.api_version, "get") }
describe "#versions" do
subject { request.versions.get }

subject do
expect(request).to receive(:perform_request)
.with(request.api_url("versions"))
.and_return(fixtures)

request.send("versions").get
end
before(:each) { stub_request(request, 'versions', 'versions') }

it "returns an Array" do
expect(subject).to be_an(Array)
Expand Down
92 changes: 53 additions & 39 deletions spec/lol/stats_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,90 @@
include Lol

describe StatsRequest do
let(:request) { StatsRequest.new "api_key", "euw" }
let(:request) { StatsRequest.new("api_key", "euw") }

it "inherits from Request" do
expect(GameRequest.ancestors[1]).to eq(Request)
end

describe "#summary" do
let(:fixture) { load_fixture 'stats', StatsRequest.api_version, 'get' }

subject do
expect(request.class).to receive(:get).with(request.api_url("stats/by-summoner/1/summary")).and_return fixture

request.summary 1
end

it 'requires a summoner' do
expect { request.summary }.to raise_error ArgumentError
end

it 'returns an array' do
expect(subject).to be_a Array
it 'raises an error when unexpected parameter is received' do
expect { request.summary '1', asd: 'foo' }.to raise_error ArgumentError
end

it 'returns an array of PlayerStatistic' do
expect(subject.map(&:class).uniq).to eq [PlayerStatistic]
end
context 'with summoner' do
subject { request.summary(1) }

it 'fetches PlayerStatistics from the API' do
expect(subject.size).to eq load_fixture('stats', StatsRequest.api_version, 'get')['playerStatSummaries'].size
end
context 'without season' do
let(:fixture) { load_fixture('stats', StatsRequest.api_version, 'get') }

it 'optionally accepts a season' do
expect(request.class).to receive(:get).with(request.api_url('stats/by-summoner/1/summary', season: '1')).and_return fixture
request.summary '1', season: '1'
end
before(:each) { stub_request(request, 'stats', 'stats/by-summoner/1/summary') }

it 'returns an array' do
expect(subject).to be_a Array
end

it 'returns an array of PlayerStatistic' do
expect(subject.map(&:class).uniq).to eq [PlayerStatistic]
end

it 'fetches PlayerStatistics from the API' do
expect(subject.size).to eq(fixture['playerStatSummaries'].size)
end
end

context 'with season' do
before(:each) { stub_request(request, 'stats', 'stats/by-summoner/1/summary', season: '1') }

it 'optionally accepts a season' do
request.summary('1', season: '1')
end
end

it 'raises an error when unexpected parameter is received' do
expect { request.summary '1', asd: 'foo' }.to raise_error ArgumentError
end
end

describe "#ranked" do
let(:fixture) { load_fixture 'ranked_stats', StatsRequest.api_version, 'get' }

subject do
expect(request.class).to receive(:get).with(request.api_url("stats/by-summoner/1/ranked")).and_return fixture
request.ranked 1
end

it 'requires a summoner' do
expect { request.ranked }.to raise_error ArgumentError
end

it 'returns a RankedStatisticsSummary' do
expect(subject).to be_a RankedStatisticsSummary
it 'raises an error when unexpected parameter is received' do
expect { request.ranked '1', asd: 'foo' }.to raise_error ArgumentError
end

it 'fetches RankedStatisticsSummary from the API' do
expect(subject.champions.size).to eq load_fixture('ranked_stats', StatsRequest.api_version, 'get')['champions'].size
end
context 'with summoner' do
let(:fixture) { load_fixture 'ranked_stats', StatsRequest.api_version, 'get' }

it 'optionally accepts a season' do
expect(request.class).to receive(:get).with(request.api_url('stats/by-summoner/1/ranked', season: '1')).and_return fixture
context 'without season' do
subject { request.ranked(1) }

request.ranked '1', season: '1'
end
before(:each) { stub_request(request, 'ranked_stats', 'stats/by-summoner/1/ranked') }

it 'raises an error when unexpected parameter is received' do
expect { request.ranked '1', asd: 'foo' }.to raise_error ArgumentError
it 'returns a RankedStatisticsSummary' do
expect(subject).to be_a RankedStatisticsSummary
end

it 'fetches RankedStatisticsSummary from the API' do
expect(subject.champions.size).to eq(fixture['champions'].size)
end
end

context 'with season' do
before(:each) { stub_request(request, 'ranked_stats', 'stats/by-summoner/1/ranked', season: '1') }

it 'optionally accepts a season' do
request.ranked('1', season: '1')
end
end
end

end

end

0 comments on commit 84fab1b

Please sign in to comment.