Sha256: 1ada00052db65d842191fd793d6595867efc294cecef76131fcc05a04f492eae

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

require "spec_helper"

describe TriviaCrack::API::User do

  let(:session) { TriviaCrack::Session.new session_id: "session", user_id: 1 }
  let(:client) { (Class.new(APIStub) { include TriviaCrack::API::User }).new session }

  let(:response) { double(status: code, body: raw_data) }

  before { allow(Faraday).to receive(:get) { response } }

  describe "#get_user_id" do
    let(:username) { "example.2" }

    subject { client.get_user_id username }

    let(:raw_data) { SpecData.get "search.json" }

    context 'given that the request is successful' do
      let(:code) { 200 }

      it { is_expected.to be 112 }
    end

    context 'given that the request fails' do
      let(:code) { 400 }

      it { expect{ subject }.to raise_error TriviaCrack::Errors::RequestError }
      it { expect{ subject }.to raise_error(an_instance_of(TriviaCrack::Errors::RequestError)
        .and having_attributes(code: code)) }
      it { expect{ subject }.to raise_error(an_instance_of(TriviaCrack::Errors::RequestError)
        .and having_attributes(url: "/api/search?username=#{username}")) }
    end
  end

  describe "#get_user" do

    subject { client.get_user }

    let(:raw_data) { SpecData.get "dashboard.json" }

    context 'given that the request is successful' do
      let(:code) { 200 }

      it { expect(TriviaCrack::Parsers::UserParser).to receive(:parse).once; subject }
      it { is_expected.to be_a TriviaCrack::User }
    end

    context 'given that the request fails' do
      let(:code) { 400 }

      it { expect{ subject }.to raise_error TriviaCrack::Errors::RequestError }
      it { expect{ subject }.to raise_error(an_instance_of(TriviaCrack::Errors::RequestError)
        .and having_attributes(code: code)) }
      it { expect{ subject }.to raise_error(an_instance_of(TriviaCrack::Errors::RequestError)
        .and having_attributes(url: "/api/users/#{session.user_id}")) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
triviacrack-0.6.0 spec/api/user_spec.rb
triviacrack-0.5.1 spec/api/user_spec.rb
triviacrack-0.5.0 spec/api/user_spec.rb