From 55ee72a5c368e1f999bf60f3a4672cba24ae798c Mon Sep 17 00:00:00 2001 From: Hannes Hornwall Date: Thu, 14 May 2020 17:07:59 +0200 Subject: [PATCH] Add specs to client and response (#5) * Add spec to client * Add specs for response * attr_reader --- lib/postnord/response.rb | 8 +++----- postnord.gemspec | 1 + spec/postnord/client_spec.rb | 16 ++++++++++++++++ spec/postnord/response_spec.rb | 28 ++++++++++++++++++++++++++++ spec/spec_helper.rb | 1 + 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 spec/postnord/client_spec.rb create mode 100644 spec/postnord/response_spec.rb diff --git a/lib/postnord/response.rb b/lib/postnord/response.rb index 1dc6f64..ae3b2ed 100644 --- a/lib/postnord/response.rb +++ b/lib/postnord/response.rb @@ -1,14 +1,12 @@ module Postnord class Response + attr_reader :code + def initialize(data) @data = data @code = data.status end - def code - @data.code - end - def data JSON.parse(@data.body) rescue JSON::ParserError @@ -17,7 +15,7 @@ def data def to_h { - code: @code, + code: code, data: data, } end diff --git a/postnord.gemspec b/postnord.gemspec index 436427d..1414548 100644 --- a/postnord.gemspec +++ b/postnord.gemspec @@ -19,6 +19,7 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency 'faraday' + spec.add_development_dependency "webmock" spec.add_development_dependency "bundler" spec.add_development_dependency "rake", ">= 10.0" spec.add_development_dependency "rspec", "~> 3.0" diff --git a/spec/postnord/client_spec.rb b/spec/postnord/client_spec.rb new file mode 100644 index 0000000..53ce8a7 --- /dev/null +++ b/spec/postnord/client_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +RSpec.describe Postnord::Client do + subject { Postnord::Client.new(api_version: 'v1', api_key: 'test', api_endpoint: 'test_endpoint', locale: 'sv', return_type: 'json') } + + before do + stub_request(:get, /test_endpoint/).to_return(status: 200, body: '{ "test": "data" }', headers: {}) + end + + it 'does the request' do + res = subject.do_request('service', 'endpoint') + + expect(res.code).to eq(200) + expect(res.data).to eq({ 'test' => 'data' }) + end +end diff --git a/spec/postnord/response_spec.rb b/spec/postnord/response_spec.rb new file mode 100644 index 0000000..b502327 --- /dev/null +++ b/spec/postnord/response_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +RSpec.describe Postnord::Response do + let(:body) { { 'test' => 'body' } } + let(:data) { OpenStruct.new(status: 200, body: body.to_json) } + subject { Postnord::Response.new(data) } + + describe '#code' do + it 'returns the status from data' do + expect(subject.code).to eq(200) + end + end + + describe '#data' do + it 'returns the desirialized json body' do + expect(subject.data).to eq(body) + end + end + + describe 'to_h' do + it 'returns a hash based on the given data' do + expect(subject.to_h).to eq({ + code: 200, + data: body + }) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9614178..2aaee33 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ require 'bundler/setup' +require 'webmock/rspec' require 'simplecov'